Insert data from an array to another array

1
Arreglouno = {"Bélgica", "México", "Nicaragua", "Alemania " }

// this is the first fix already with data

String arreglodos = new String[2];
Int i=0;
for (i = 0; i < Arreglouno.length;i++) {
  if ( i == 1) {
         arreglodos [0] = [i];
  } 
}

The problem I have is that I can not get the second vector to be filled with the data in the position of the first array.

    
asked by Julián Franco 25.10.2018 в 06:31
source

1 answer

1

It's quite easy, you must first create the second array with the same size as the first one, and with this you only have to iterate (the way you already did) and copy the i element of array1 to the position i of fix2:

    String arreglo1[] = {"Bélgica", "México", "Nicaragua", "Alemania "};
    String arreglo2[] = new String[arreglo1.length];
    for(int i = 0; i < arreglo1.length; i++){
        arreglo2[i] = arreglo1[i];
    }

Now array2 contains everything from array1. Now, if you want to move only a certain number of elements you can do it in the following way:

String arreglo2[] = new String[n]; //n = número de elementos que quieras copiar
for(int i = 0; i < n; i++){
    arreglo2[i] = arreglo1[i];
}

I hope you serve, greetings.

    
answered by 25.10.2018 в 06:52