简体   繁体   English

反转二维数组

[英]Inverting a two-dimensional array

I'm trying to invert and flip a two-dimensional array, but something goes wrong! 我正在尝试反转和翻转二维数组,但是出了点问题! Flipping works ok, but inverting is not. 翻转可以,但是不能翻转。 Can't find a mistake right here: 在这里找不到错误:

public int[][] flipAndInvert(int[][] A) {
    int row = -1;
    int col = -1;
    int[][] arr = A;

    for (int i = 0; i < arr.length; i++) {
        row++;
        col = -1;
        for (int j = arr[i].length - 1; j >= 0; j--) {
            col++;
            arr[row][col] = A[i][j];
        }
    }
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            if (arr[i][j] == 1) {
                arr[i][j] = 0;
            } else {
                arr[i][j] = 1;
            }
        }
    }
    return arr;
}
int[][] A = { { 0, 1, 1 },{ 0, 0, 1 },{ 0, 0, 0 } };

After proceeding the output should be: After inverting: {1,1,0},{1,0,0},{0,0,0} After flipping: {0,0,1,},{0,1,1},{1,1,1} 继续输出之后,应该是:反转之后:{1,1,0},{1,0,0},{0,0,0}翻转之后:{0,0,1,},{0,1, 1},{1,1,1}

Thanks to all a lot, the problem was here: int[][] arr = A; 非常感谢,问题出在这里:int [] [] arr = A; The reference of the array is being passed to arr. 数组的引用将传递给arr。

What I think is that since you are using this line: 我认为是因为您正在使用此行:

int[][] arr = A;

The reference of the array is being passed to arr, and hence the line: 数组的引用被传递给arr,因此一行:

arr[row][col] = A[i][j];

is equivalent to: 等效于:

A[row][col] = A[i][j];

as arr has an reference to A and they both now refer to the same memory location (or they are both different names to a single variable) 因为arr引用了A,并且它们现在都引用相同的内存位置(或者它们是单个变量的不同名称)

You can fix this by either using the new keyword with arr and then initializing it: 您可以通过使用带有arr的new关键字然后对其进行初始化来解决此问题:

int[][] arr = new int[someRows][someCols];
//use for loop to assign the value to each element of arr

Or you can run the for loop till arr[i].length/2 - 1: 或者,您可以运行for循环直到arr [i] .length / 2-1:

    for (int i = 0; i < arr.length; i++) {
    row++;
    col = -1;
    for (int j = arr[i].length / 2 - 1; j >= 0; j--) { //here changed arr[i].length to arr[i].length / 2
        col++; 
        arr[row][col] = A[i][j]; //for this you do not need arr and you can directly work on A and return it
    }
}

The problem of your code should be this line: 您的代码的问题应该是这一行:

int[][] arr = A;

You are assigning the reference of array A to arr and from then when you modify one you modify both of the arrays or better they are modified together because they refer to the same address. 您正在将数组A的引用分配给arr ,从那时起,当您修改数组A时,您将同时修改两个数组,或者最好同时修改它们,因为它们引用的是同一地址。

Using apache commons lang: 使用apache commons lang:

    int[][] matrix = new int[3][3];
    for (int i = 0; i < matrix.length; i++) {
        matrix[i][0] = 3 * i;
        matrix[i][1] = 3 * i + 1;
        matrix[i][2] = 3 * i + 2;
    }

    System.out.println(Arrays.toString(matrix[0]));
    System.out.println(Arrays.toString(matrix[1]));
    System.out.println(Arrays.toString(matrix[2]));

    ArrayUtils.reverse(matrix);

    System.out.println(Arrays.toString(matrix[0]));
    System.out.println(Arrays.toString(matrix[1]));
    System.out.println(Arrays.toString(matrix[2]));

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM