Aldione
Aldione

Reputation: 29

How to copy array dimensional

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.

  1. 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

Answers (2)

Aldione
Aldione

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

GhostCat
GhostCat

Reputation: 140427

Actually, the answer is pretty simple, you only need to do these three things:

  • create a new array, down to the last dimension, like you alrady do with int valor[][][]=new int[datos][datos2][datos3];
  • the above gives you that "3 dim" array, with all elements pre-setted to 0 (because it is an array of int, there can't be NULL values in there)
  • then you iterate your "old" array, and for each value, you "simply" decide where it should go to in the new, larger array

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

Related Questions