把二维数组中元素逆序以二维数组方式返回,在数组中直接做逆序操作也是一样的
public static int[][] reverse(int[][] arr) {
int[] arr1 = new int[length];
int[][] arr2 = new int[arr.length][];
int index1 = 0;
for (int i = 0;i < arr.length ;i ++ ) {
for (int j = 0;j < arr[i].length;j ++ ) {
arr1[index1] =arr[i][j];
index++;
}
}
for (int x = 0;x < arr1.length/2 ;x ++ ) {
int temp = arr1[x];
arr1[x] = arr1[arr1.length-1-x];
arr1[arr1.length-1-x] = temp;
}
int index2 = 0;
for (int y = 0;y < arr.length ;y ++ ) {
for (int z = 0;z < arr[y].length ;z ++ ) {
arr2[y][z] = arr1[index2];
index2++;
}
}
return arr2;
}
|
|