Java 反转二维数组的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21920939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:24:41  来源:igfitidea点击:

Reverse the rows of a 2d array

javaarraysmultidimensional-array

提问by iHeff

Yesterday I asked a very similar question and I kind of messed up with asking it.

昨天我问了一个非常相似的问题,我问它有点搞砸了。

I need to pass an array to a method and inside of that method I need to swap the rows around so if it's

我需要将一个数组传递给一个方法,在该方法内部我需要交换行,所以如果它是

1 2 3

1 2 3

3 2 1

3 2 1

2 1 3

2 1 3

it needs to be

它需要是

3 2 1

3 2 1

1 2 3

1 2 3

3 1 2

3 1 2

With the code I have right now it swaps the last column to the first column spot correctly then it puts the column that's supposed to be last.

使用我现在拥有的代码,它将最后一列正确交换到第一列位置,然后将应该放在最后的列放置。

3 1 2

3 1 2

1 3 2

1 3 2

3 2 1

3 2 1

Also, it needs to stay a void because I need to be modifying the original array so I can't set it as a temp array but I can use a temp integer to store.

此外,它需要保持为空,因为我需要修改原始数组,所以我不能将它设置为临时数组,但我可以使用临时整数来存储。

Here is the code I have right now that's sort of working

这是我现在可以使用的代码

public static void reverseRows(int[][] inTwoDArray)
{
   for (int row = 0; row < inTwoDArray.length; row++)
   {
       for (int col = 0; col < inTwoDArray[row].length; col++)
       {
           int tempHolder = inTwoDArray[row][col];
           inTwoDArray[row][col] = inTwoDArray[row][inTwoDArray[0].length - 1];
           inTwoDArray[row][inTwoDArray[0].length - 1] = tempHolder;
       }
    }
 }

any help would be great, I'm running out of hair to pull out! Thanks!

任何帮助都会很棒,我的头发快要拔出来了!谢谢!

采纳答案by Kuba Spatny

First, how to reverse a single 1-D array:

首先,如何反转单个一维数组:

for(int i = 0; i < array.length / 2; i++) {
    int temp = array[i];
    array[i] = array[array.length - i - 1];
    array[array.length - i - 1] = temp;
}

Note that you must stop in half of your array or you would swap it twice (it would be the same one you started with).

请注意,您必须停在阵列的一半处,否则您将交换它两次(这将与您开始时使用的相同)。

Then put it in another for loop:

然后把它放在另一个 for 循环中:

for(int j = 0; j < array.length; j++){
    for(int i = 0; i < array[j].length / 2; i++) {
        int temp = array[j][i];
        array[j][i] = array[j][array[j].length - i - 1];
        array[j][array[j].length - i - 1] = temp;
    }
}

Another approach would be to use some library method such as from ArrayUtils#reverse():

另一种方法是使用一些库方法,例如 from ArrayUtils#reverse()

ArrayUtils.reverse(array);

And then again put into a cycle:

然后再次放入一个循环:

for(int i = 0; i < array.length; i++){
   ArrayUtils.reverse(array[i]);
}

回答by Johannes H.

Not sure if I didn't cofuse what array stores the rows and which one the columns.... but this shoudl work (long time since I've done Java last, so be nice to me when spotting any errors please ^^):

不确定我是否没有混淆存储行的数组和存储列的数组....但这应该可以工作(自从我上次完成 Java 以来已经很长时间了,所以在发现任何错误时请对我好点^^) :

public static void reverseRows(int [][] array)
{
    for (int i = 0 ; i < array.length ; i++) {       // for each row...
        int[] reversed = new int[array[i].length];   // ... create a temporary array that will hold the reversed inner one ...
        for(int j = o ; j < array[i].length ; j++) { // ... and for each column ...
            reversed[reversed.length - 1 - j] = array[i][j]; // ... insert the current element at the mirrored position of our temporary array
        }
        array[i] = reversed; // finally use the reversed array as new row.
    }
}

回答by Nikhil Kumar

Java Code :-

Java代码:-

import java.util.Scanner;

public class Rev_Two_D {

    static int col;
    static int row;
    static int[][] trans_arr = new int[col][row];

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        row = m;
        int n = sc.nextInt();
        col = n;
        int[][] arr = new int[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                arr[i][j] = sc.nextInt();
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        for (int j = 0; j < arr.length; j++) {
            for (int i = 0; i < arr[j].length / 2; i++) {
                int temp = arr[j][i];
                arr[j][i] = arr[j][arr[j].length - i - 1];
                arr[j][arr[j].length - i - 1] = temp;
            }
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

回答by Krishna Kumar Chourasiya

Reverse of two D array - Print your two D array in reverse order

两个 D 数组的反转 - 以相反的顺序打印两个 D 数组

public void reverse(){
    int row = 3;
    int col = 3;
    int[][] arr = new int[row][col];
    int k=0;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++,k++) {
            arr[i][j] = k;
            System.out.print(arr[i][j] + " ");
        }
        System.out.println();
    }
    System.out.println();
    for (int i = arr.length -1; i >=0 ; i--) {
        for (int j = arr.length -1; j >=0 ; j--) {
            System.out.print(arr[i][j] + " ");
        }
        System.out.println();
    }
}

回答by Shivaraj Yadachi

public static void main(String[] args) {

    int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

    for(int i=0 ; i<a.length;i++)
    {
        for(int j=0 ; j<a.length;j++)
        {
            System.out.print(a[i][j]+",");
        }
        System.out.println();

    }
    System.out.println("***************************");
    for(int i=0 ; i<a.length;i++)
    {
        for(int j=a.length-1 ; j>=0;j--)
        {
            System.out.print(a[i][j]+",");
        }
        System.out.println();

    }

}

回答by Bittu Philip

Reverse 2 D Array

反转二维数组

public static void main(String[] args) {

int a[][] =  {{1,2,3},
              {4,5,6},
              {8,9,10,12,15}
              };

for(int i=0 ; i<a.length;i++)
{
    for(int j=0 ; j<a[i].length;j++)
    {
        System.out.print(a[i][j]+",");
    }
    System.out.println();

}

for(int i=0 ; i<a.length;i++)
{
    for(int j=a[i].length-1 ; j>=0;j--)
    {
        System.out.print(a[i][j]+",");
    }
    System.out.println();

}

回答by RITIKA DHAMIJA

I guess this the easiest approach, tried and tested For instance, you have

我想这是最简单的方法,经过尝试和测试 例如,你有

1 2
3 4

and you want

你想要

2 1 
4 3 

You can reverse the loop, without any extra space or inbuilt function. Solution:

您可以反转循环,无需任何额外空间或内置函数。解决方案:

for(int i =0;i<arr.length;i++) //arr.length=no of rows
{
  for(int j = arr[i].length-1;j>=0;j--)//arr[i].length=no of col in a ith row
    {
        System.out.print(arr[i][j]+" ");
    }
    System.out.println();
}