Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 问题:反转二维数组的行_Java_Arrays_Reverse - Fatal编程技术网

Java 问题:反转二维数组的行

Java 问题:反转二维数组的行,java,arrays,reverse,Java,Arrays,Reverse,我试图用偶数索引反转行,但不是写这样的东西: 654321 它是这样写的: 65456: 如何解决这个问题 p.S.代码如下 int[][] a = new int[6][6]; int k = 1; for(int i = 0; i < 6 ; i++) { for(int j = 0; j < 6 ; j++) { a[i][j]=k; k++; } }

我试图用偶数索引反转行,但不是写这样的东西:

654321

它是这样写的:

65456:

如何解决这个问题

p.S.代码如下

int[][] a = new int[6][6];
    int k = 1;
    for(int i = 0; i < 6 ; i++)
    {
        for(int j = 0; j < 6 ; j++)
        {
            a[i][j]=k;
            k++;
        }
    }

    for(int i = 0; i < 6 ; i++)
    {
        for(int j = 0; j < 6 ; j++)
        {
            if(i%2 == 0)
            {
                int temp = a[i][j];
                a[i][j] = a[i][a.length - 1 -j];
                a[i][a.length - 1 - j] = temp;
            }
                System.out.print(a[i][j] + "\t");           
        }
        System.out.println();
    }

循环应该一直运行到每行的中间。。。然后交换元素

// define the array
int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// print it
System.out.println(Arrays.deepToString(a));

// loop all the rows
for (int i = 0; i < a.length; i++) {
    // loop every element on the row until the middle
    for (int j = 0; j < a[i].length / 2; j++) {
        // swap those
        int temp = a[i][j];
        a[i][j] = a[i][a[i].length - j - 1];
        a[i][a[i].length - j - 1] = temp;
    }
}
// print it again
System.out.println(Arrays.deepToString(a));

如果你真的想学习编程——用调试程序运行你的代码。使用方法和好的变量名,将问题分解成更简单的部分:对于int rowIndex=0;rowIndex