反转2d数组的行 [英] Reverse the rows of a 2d array

查看:171
本文介绍了反转2d数组的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



1 2 3



3 2 1



2 1 3



它需要是

3 2 1



1 2 3



3 1 2



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



3 1 2



1 3 2



3 2 1



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



这里是我现在的代码,有点工作

  public static void reverseRows(int [] [] inTwoDArray)
{
f或(int row = 0;行< 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;
}
}
}

任何帮助都会很棒,我的头发已用完了!谢谢!

解决方案

首先,如何反转单个1-D阵列:

  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;
}

请注意,您必须停在一半的数组中,否则您需要交换两次(它与你开始的那个相同)。



然后把它放在另一个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;
}
}

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

  ArrayUtils.reverse(array); 

然后又进入一个周期:

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


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

3 2 1

2 1 3

it needs to be

3 2 1

1 2 3

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

1 3 2

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!

解决方案

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(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():

ArrayUtils.reverse(array);

And then again put into a cycle:

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

这篇关于反转2d数组的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆