二维数组逆序

来源:互联网 发布:太空工程师 编程教程 编辑:程序博客网 时间:2024/05/24 06:04

今天,我弄了弄二维数组逆序来练习

我们需要从后往前遍历二维数组,并将里面的元素放到一个新数组




// 二维数组逆序public static void outOf() {int[][] old = { { 31, 32, 48, 37 }, { 41, 30, 97, 73 }, { 13, 34, 92, 37 } };int[][] newa = new int[3][4];for (int i = old.length - 1; i >= 0; i--) {for (int j = old[i].length -1; j >= 0; j--) {newa[old.length - 1 - i][old[i].length - 1 - j] = old[i][j];}}for (int[] row : old) {for (int val : row) {System.out.print(val + " ");}System.out.println();}System.out.println("-----------------");for (int[] row : newa) {for (int val : row) {System.out.print(val + " ");}System.out.println();}}




没什么难的,主要是遍历的时候新数组的下标计算