The next code creates a random multidimensional array, named 'arista', and fills each of the slots of the md-array with integers.
Then, it prints it. And you can see how are the slots filled with integers. Then I tried to create an array, called 'lista', which should be a list containing ALL the values stored in the multidimensional array, by typing:
System.out.printf(Arrays.toString(lista));
But the result is not what I expected. Only the last row of 'arista' appears in the array 'lista' and the other portion of the slots of the array 'lista' are zeroes.
How could I correct this? what is wrong?
The complete code is:
public static void main(String[] args) {
int renglones = (int) (Math.random() * 5) + 5;
int columnas = (int) (Math.random() * 5) + 5;
int[][] arista = new int[renglones][columnas];
int[] lista;
lista = new int[renglones * columnas];
int k = 1;
for (int i=0; i < renglones; i++ ){
for (int j=0; j < columnas; j++) {
arista[i][j] = k++;
lista[j] = arista[i][j];
}
}
for (int i = 0; i < renglones; i++) {
for (int j = 0; j < columnas; j++) {
System.out.printf("[%d][%d] = %d \n", i, j, arista[i][j]);
}
System.out.println();
}
System.out.printf(Arrays.toString(lista));
}