Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.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 - Fatal编程技术网

Java 如何对二维数组的列重新排序?

Java 如何对二维数组的列重新排序?,java,Java,有没有一种简单的方法可以随机排列二维数组中的列顺序?例如,我可以使用它来随机化这个2d数组的列吗 int[][] dataArray = {{1, 1, 0, 1, 0, 0}, {0, 0, 1, 0, 1, 1}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 1, 1}}; 我已经尝试了一段时间,但迄今为止运气不佳。 我想做的是让列12345

有没有一种简单的方法可以随机排列二维数组中的列顺序?例如,我可以使用它来随机化这个2d数组的列吗

int[][] dataArray = {{1, 1, 0, 1, 0, 0}, 
                     {0, 0, 1, 0, 1, 1},
                     {1, 1, 1, 1, 1, 1}, 
                    {0, 0, 0, 0, 1, 1}};
我已经尝试了一段时间,但迄今为止运气不佳。 我想做的是让列123456随机变成列2316544。
我曾尝试随机绘制数字,并尝试使列与顺序匹配,但我似乎无法实现这一点。

您始终可以构建一个1D数组,随机化其内容,然后重建一个2D数组

int[] newArray = new int[a.length + b.length + c.length + d.length]; 
// where a, b, c, and d are the smaller arrays inside dataArray
System.arrayCopy(a, 0, newArray, 0, a.length);
System.arrayCopy(b, 0, newArray, a.length, b.length);
System.arrayCopy(c, 0, newArray, a.length + b.length, c.length);
System.arrayCopy(d, 0, newArray, a.length + b.length + c.length, d.length);
这将构造一个名为
newArray
的一维数组,其中包含所有较小数组的内容

现在将该数组随机化,如图所示

然后,要重建原始阵列,请执行以下操作:

int newArray2d[][] = new int[5][4]; //do this dynamically based on the size of the first array.
for(int i=0; i<5;i++)
   for(int j=0;j<4;j++)
       array2d[i][j] = array1d[(j*10) + i]; 
int newArray2d[]]=newint[5][4]//根据第一个数组的大小动态执行此操作。

对于(int i=0;我能不能简单地将所有数字添加到一个数组中,随机化该数组的顺序,然后将其切成6个元素片段并重建2d数组?很抱歉,这是我第一次处理2d数组,我不知道具体怎么做。第1、2和4列是相同的。第5和6列也是相同的。为什么要对其进行d这是我工作任务的一部分