Java offers different ways to copy content from one array to another:
Implementing a loop with the for
Using the clone
Using System.arraycopy
Using Arrays.copyOf
The most appropriate strategy in this case is arraycopy
where you can define exactly the positions you want to copy with just one row, the best performance, and no need to import any library additional, that is, 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: link
The only thing that may be stranger in using arraycopy
are the parameters that might confuse you, but here is a brief explanation of what each means in the order 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);
vector1 will be copied, from position zero , to vector3 , starting at position zero , by making a full size ( length length ) copy of vector1 .
System.arraycopy(vetor2, 0, vetor3, 3, vetor2.length);
vector2 will be copied, from position zero , to vector3 , starting at position three , by making a full size ( length length ) copy of vector2 .
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()
is very simple:
int [] vetorB = vetorA.clone();
Using the java.util.Arrays library you can use copyOf
:
import java.util.Arrays;
int [] vetorB = Arrays.copyOf(vetorA, vetorA.length);
More information:
link
I hope this helps ...
Good Luck!