简体   繁体   中英

Is my code for mirror image of a 2D array correct?

public int [][] Mirror(int [][] A1){ 
    int [][] A2 = new int [A1.length][];
    for(int i = 0; i < A1.length; i++) {
        for(int j = A1[i].length-1; j >= 0; j--) {
            A2[i][j] = A1[i][j];
        }
    }
    return A2;
}

I am not sure what is wrong with this code. Any help will be appreciated. Thank you.

A2[i][j] = A1[i][j] assigns the exact same values of the input array to the same indices in the output array, so the output array would be identical to the input array.

It should be

A2[i][j] = A1[i][A1[i].length-1-j];

This way, for example, the last column of each row in the input array becomes the first column in the output array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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