How to invest multidimensional arrays in java?

1

I need to invert the values of an array, where if for example I get

  

1 5 2
4 1 5
9 4 6

See me like this,

  

9 4 6
4 1 5
1 5 2


This is my method, it prints certain values in 0, I need to see what it can be
are 2 vectors, vector2d1 the main one from which I draw the values and the aux to which I keep them vector2d2

for (int i = 0; i < vector2d1.length /2; i++) {
    for (int c = 0; c < vector2d1.length  ; c++) {
       vector2d2[i][c] = vector2d1[i][c];
       vector2d1[i][c] = vector2d1[vector2d1.length - 1][c];
       vector2d1[vector2d1.length - 1][c] = vector2d2[i][c];
    }
}
    
asked by Emmanuel Burciaga 25.10.2017 в 04:39
source

3 answers

2

You almost have it ready ... only you do not exchange properly with a "temporary" variable and subtract the pointer i so you can invert the data the way you expect.

for (int i = 0; i < vector2d1.length /2; i++) {
    for (int c = 0; c < vector2d1.length  ; c++) {
       int temp = vector2d1[i][c];
       vector2d1[i][c] = vector2d1[vector2d1.length - 1 - i][c];
       vector2d1[vector2d1.length - 1 - i][c] = temp;
    }
}

The great aggravating factor of this code is that it will fail when the structure of the array is not of equal dimensions, that is, when it is not equal in rows and columns.

verctor2d1.length as it will return the size in rows, only that, will not give you information about the width of the array 2d, therefore, when it is an array of identical dimensions it will not matter because rows and columns are the same, all ok, but if we have for example a vector2d1[3][5] the second cycle for the person in charge of traversing columns will only get up to 3, not up to 5, you would ignore 2 columns of data !; This behavior is possible to correct for example with vector2d1[1].length , which gives the width.

Going back to your code and now modifying that second cycle for :

for (int i = 0; i < vector2d1.length /2; i++) {
    for (int c = 0; c < vector2d1[1].length; c++) {
       int temp = vector2d1[i][c];
       vector2d1[i][c] = vector2d1[vector2d1.length - 1 - i][c];
       vector2d1[vector2d1.length - 1 - i][c] = temp;
    }
}

The program is more generic by inverting the rows of data the way you plan.

    
answered by 25.10.2017 в 07:45
0

Method that solves your example:

public static void inversorArray(Object[][] arreglo) {
    for(int i = 0; i < (arreglo.length / 2); i++) {
        Object[] temp = arreglo[i];
        arreglo[i] = arreglo[arreglo.length - i - 1];
        arreglo[arreglo.length - i - 1] = temp;
    }
}
    
answered by 25.10.2017 в 04:50
0

Hey, what a friend!

This is what I understood you.

int[][] r = new int[3][3];
int[][] r2 = new int[3][3];
int cont = 1;
for (int i = 0; i < r.length; i++){
    for (int j = 0; j < r.length; j++) {
        r[i][j] = cont;
        System.out.print(" " +r[i][j]);
        cont++;
    }
    System.out.println();
}
System.out.println();

The previous code was to fill the arrangement, what works for you is as follows:

for (int i = r.length-1; i >=0; i--) {
    for (int j = r.length-1; j >= 0; j--) {
        r2[i][j] = r[i][j];
        System.out.print(" " + r[i][j]);

    }
    System.out.println();
}

Answer:

 1 2 3
 4 5 6
 7 8 9

 9 8 7
 6 5 4
 3 2 1

I hope you serve, salu2!

    
answered by 29.10.2017 в 04:46