猿问

在java中复制二维数组的每第二行和每第二列

我有一个二维整数数组,我需要使用 java 复制二维数组的每一行和每一列,如下所示(覆盖现有值,而不是创建新列/行):


当前方法


     for(int i = 0; i < result.get(z).getWidth()-2; i+=1)

    {   for(int j = 0; j < result.get(z).getHeight()-2; j+=1)

    {   

        x[i+1][j] = result.get(z).getRGB(i, j);

        x[i][j+1] = result.get(z).getRGB(i, j);

        x[i+1][j+1] = result.get(z).getRGB(i, j);





    }


}


繁花不似锦
浏览 133回答 2
2回答

神不在的星期二

if/else 的另一种方法:private static void duplicateAlternatesWithIfElse(int[][] matrix) {&nbsp; &nbsp; &nbsp; &nbsp; for (int row = 0; row < matrix.length; row++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int col = 0; col < matrix.length; col++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((row - 1) % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix[row][col] = matrix[row - 1][col];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((col - 1) % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix[row][col] = matrix[row][col - 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }使用主函数测试:public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int[][] twoDimArray = {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 10, 17, 200, 255, 0 },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0, 199, 100, 30, 45 },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 250, 23, 177, 180, 79 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 0, 9, 14, 150, 70 },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 50, 55, 187, 10, 233 }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; printMatrix(twoDimArray);&nbsp; &nbsp; &nbsp; &nbsp; duplicateAlternatesWithIfElse(twoDimArray);&nbsp; &nbsp; &nbsp; &nbsp; printMatrix(twoDimArray);&nbsp; &nbsp; }输出:&nbsp; 10&nbsp; 17 200 255&nbsp; &nbsp;0&nbsp; &nbsp;0 199 100&nbsp; 30&nbsp; 45&nbsp;250&nbsp; 23 177 180&nbsp; 79&nbsp; &nbsp;0&nbsp; &nbsp;9&nbsp; 14 150&nbsp; 70&nbsp; 50&nbsp; 55 187&nbsp; 10 233&nbsp; 10&nbsp; 10 200 200&nbsp; &nbsp;0&nbsp; 10&nbsp; 10 200 200&nbsp; &nbsp;0&nbsp;250 250 177 177&nbsp; 79&nbsp;250 250 177 177&nbsp; 79&nbsp; 50&nbsp; 50 187 187 233

大话西游666

替代方法:private static void duplicateAlternates(int[][] matrix) {&nbsp; &nbsp; for (int row = 0; row < matrix.length; row += 2)&nbsp; &nbsp; &nbsp; &nbsp; for (int col = 1; col < matrix.length; col += 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix[row][col] = matrix[row][col - 1];&nbsp; &nbsp; for (int row = 1; row < matrix.length; row += 2)&nbsp; &nbsp; &nbsp; &nbsp; for (int col = 0; col < matrix.length; col++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix[row][col] = matrix[row - 1][col];}测试public static void main(String[] args) {&nbsp; &nbsp; int[][] matrix = { {&nbsp; 10,&nbsp; 17, 200, 255,&nbsp; &nbsp;0 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp;0, 199, 100,&nbsp; 30,&nbsp; 45 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 250,&nbsp; 23, 177, 180,&nbsp; 79 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp;0,&nbsp; &nbsp;9,&nbsp; 14, 150,&nbsp; 70 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; 50,&nbsp; 55, 187,&nbsp; 10, 233 } };&nbsp; &nbsp; print(matrix);&nbsp; &nbsp; duplicateAlternates(matrix);&nbsp; &nbsp; print(matrix);}private static void print(int[][] matrix) {&nbsp; &nbsp; for (int row = 0; row < matrix.length; row++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int col = 0; col < matrix[row].length; col++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf(" %3d", matrix[row][col]);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}输出&nbsp; 10&nbsp; 17 200 255&nbsp; &nbsp;0&nbsp; &nbsp;0 199 100&nbsp; 30&nbsp; 45&nbsp;250&nbsp; 23 177 180&nbsp; 79&nbsp; &nbsp;0&nbsp; &nbsp;9&nbsp; 14 150&nbsp; 70&nbsp; 50&nbsp; 55 187&nbsp; 10 233&nbsp; 10&nbsp; 10 200 200&nbsp; &nbsp;0&nbsp; 10&nbsp; 10 200 200&nbsp; &nbsp;0&nbsp;250 250 177 177&nbsp; 79&nbsp;250 250 177 177&nbsp; 79&nbsp; 50&nbsp; 50 187 187 233
随时随地看视频慕课网APP

相关分类

Java
我要回答