The Java offers different ways to copy the contents of a array
to another:
- Implementing a loop with the command for
- Using the method clone
- Using System.arraycopy
- Using Arrays.copyOf
The most appropriate strategy in this House is arraycopy
in which you can set exactly the positions you want to copy with just one line, with the best performance and without the need to import any additional library, ie, easy and efficient.
public class Exercicio6 {
public static void main(String[] args) {
float somaV1 = 0, somaV2 = 0;
float[] vetor1 = {4.3f, 2.5f, 4.7f};
float[] vetor2 = {5.7f, 5.8f, 3.7f};
for(int i = 0; i < vetor1.length; i++) {
somaV1 += vetor1[i];
somaV2 += vetor2[i];
}
System.out.print("Vetor 1 [" + vetor1[0] + ", " + vetor1[1]);
System.out.println(", " + vetor1[2] + "] Soma = " + somaV1);
System.out.print("Vetor 2 [" + vetor2[0] + ", " + vetor2[1]);
System.out.println(", " + vetor2[2] + "] Soma = " + somaV2);
float[] vetor3 = new float[6];
if (somaV1 > somaV2) {
System.arraycopy(vetor1, 0, vetor3, 0, vetor1.length);
System.arraycopy(vetor2, 0, vetor3, 3, vetor2.length);
System.out.println("\nVetor 1 com maior soma, resultado final:\n");
System.out.print("Vetor 3 [" + vetor3[0] + ", " + vetor3[1] + ", " + vetor3[2]);
System.out.print(", " + vetor3[3] + ", " + vetor3[4] + ", " + vetor3[5] + "]");
} else {
System.arraycopy(vetor2, 0, vetor3, 0, vetor2.length);
System.arraycopy(vetor1, 0, vetor3, 3, vetor1.length);
System.out.print("\nVetor 2 com maior soma, resultado final:\n");
System.out.print("Vetor 3 [" + vetor3[0] + ", " + vetor3[1] + ", " + vetor3[2]);
System.out.print(", " + vetor3[3] + ", " + vetor3[4] + ", " + vetor3[5] + "]");
}
}
}
Run the code online: http://ideone.com/g6gKLR
The only thing that can be stranger in the use of arraycopy
are the parameters that may confuse you, but below is a brief explanation of what each means in the order in which they are passed:
Object objetoQueSeraCopiado
int posicaoInicialObjetoQueSeraCopiado
Object objetoDestino
int posicaoObjetoDestino
int tamanhoObjetoCopiado
Used in the example:
System.arraycopy(vetor1, 0, vetor3, 0, vetor1.length);
The vector 1 will be copied from the position zero, to the vector 3, starting in position zero, making a full size copy (length) of vector 1.
System.arraycopy(vetor2, 0, vetor3, 3, vetor2.length);
The vector 2 will be copied from the position zero, to the vector 3, starting in position three, making a full size copy (length) of vector 2.
To copy vectors using for
:
//Criando um vetorB do mesmo tamanho do vetorA
int [] vetorB = new int[vetorA.length];
//O vetorB recebe todos os dados do vetorA
for (int i=0; i < vetorA.length; i++) {
vetorB[i] = vetorA[i];
}
//Exibe os dados do vetorB copiados do vetorA
for (int i=0; i < vetorB.length; i++) {
System.out.println("vetorB[" + i + "] = " + vetorB[i]);
}
To make a vector copy using the native method clone()
it’s very simple:
int [] vetorB = vetorA.clone();
Through the library java.util.Arrays it is possible to use the copyOf
:
import java.util.Arrays;
int [] vetorB = Arrays.copyOf(vetorA, vetorA.length);
More information:
Welcome to the site. What you have tried to do?
– user28595
I started programming recently, and then try not to laugh at my logic.
– zHardy
If the vectors have the same size, it can be summed with a single loop.
– user28595