Reputation: 29
this is my first post, i always try to find the solution but i didnt find it.
I need to copy one array[][][] to copy[][][] but the copy is need to be more longer than the array.
I wrote this but dont copy like 0, just copy like NULL and made a exception.
Copia un array tridimensional de enteros a otro de mayor tamaño.
int datos, datos2, datos3, datos4;
System.out.println("De cuantos elementos quiere que sea el array?");
datos=leerInt();
System.out.println("De cuantos elementos quiere que sea el array bidimensional?");
datos2=leerInt();
System.out.println("De cuantos elementos quiere que sea el array tridimensional?");
datos3=leerInt();
int valor[][][]=new int[datos][datos2][datos3];
for (int i=0;i<valor.length;i++){
for (int j=0;j<valor[i].length;j++){
for (int k=0;k<valor[i][j].length;k++){
System.out.println("Introduce el valor para el array"+(i+1)+" de la parte bidimensional "+(j+1)+" de la parte tridimensional "+(k+1));
datos4=leerInt();
valor[i][j][k]=datos4;
}
}
}
int copia[][][]=Arrays.copyOf(valor, valor.length+2);
for (int i=0; i < copia.length; i++) {
for (int j=0; j < copia[i].length; j++) {
for (int k=0; k < copia[i][j].length; k++) {
System.out.println("Los valores copiado es: "+ copia[i][j][k]);
}
}
}
Upvotes: 1
Views: 105
Reputation: 29
Ok, thanks to @GhostCat i solved my problem
int datos, datos2, datos3, datos4;
System.out.println("De cuantos elementos quiere que sea el array?");
datos=leerInt();
System.out.println("De cuantos elementos quiere que sea el array bidimensional?");
datos2=leerInt();
System.out.println("De cuantos elementos quiere que sea el array tridimensional?");
datos3=leerInt();
int valor[][][]=new int[datos][datos2][datos3];
int copia[][][]=new int[datos][datos2+1][datos3+2];
for (int i=0;i<valor.length;i++){
for (int j=0;j<valor[i].length;j++){
for (int k=0;k<valor[i][j].length;k++){
System.out.println("Introduce el valor para el array"+(i+1)+" de la parte bidimensional "+(j+1)+" de la parte tridimensional "+(k+1));
datos4=leerInt();
valor[i][j][k]=datos4;
}
}
}
for (int i=0;i<valor.length;i++){
for (int j=0;j<valor[i].length;j++){
for (int k=0;k<valor[i][j].length;k++){
copia[i][j][k]=valor[i][j][k];
}
}
}
for (int i=0; i < copia.length; i++) {
for (int j=0; j < copia[i].length; j++) {
for (int k=0; k < copia[i][j].length; k++) {
System.out.println("Los valores copiado es: "+ copia[i][j][k]);
}
}
}
Upvotes: 1
Reputation: 140427
Actually, the answer is pretty simple, you only need to do these three things:
int valor[][][]=new int[datos][datos2][datos3];
The dimensions of the new array, and that mapping function that you need completely depend on your requirements. We can't tell you that. You intend to solve some sort of problem, so determining how many "new" elements you need, and where exactly you want to copy in the old values completely depends on what you want to achieve. Which you didnt tell us.
Upvotes: 1