数组交换 - 二维数组 [英] Array swap - 2d array

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

问题描述

我工作在一个二维阵列换指数。我似乎是在正确的轨道上,但它不是在交换我想要的方式排列。

I'm working on swapping indices in a two-dimensional array. I seem to be on the right track, but it's not swapping the array in the way I want.

第一行的索引Ĵ需要与第2行的索引被换Ĵ

The first row's index j needs to be swapped with row 2's index j:

for (int j = 0; j < array.length ; j++){  
     int temp = array[row1][j]
     array[row1][j]=array[j][row1]
     array[j][row1] = temp ;
}

如何以最佳方式处理这个任何想法,将AP preciated。

Any ideas on how to best approach this would be appreciated.

推荐答案

由于Java中的二维数组实际上是其他数组引用的数组,你可以简单地交换引用,如下所示:

As the two-dimensional array in java is actually a array of references to other arrays, you can simply swap the references as shown below:

public static void swapRows(int array[][], int rowA, int rowB) {
   int tmpRow[] = array[rowA];
   array[rowA] = array[rowB];
   array[rowB] = tmpRow;
}

/修改:编辑的答案,因为我previously misinter preTED它**

/edit: edited the answer as I previously misinterpreted it**

这篇关于数组交换 - 二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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