按列对2D整数数组进行排序 [英] Sorting 2D array of integers by column
问题描述
我需要在Java中构建一个方法,其中输入是2D整数数组,结果得到2D整数数组,其中每个元素都引用元素在列中的位置.让我用一个例子来解释.考虑将5x5的2D数组作为该方法的输入,如下所示:
int [] [] array = new int [] [] {{124,188,24,254,339},{0,7,77,145,159},{206,340,280,523,433},{310,265,151,411,398},{24,104,0,183,198}};
现在,我需要根据以下内容构建一个新的2D整数数组(我将在下文中调用 newArray
)
-
数组中第0列的最小值为0并与第1行关联(然后,我需要在
newArray [0] [0]
中分配1)./p> -
接下来,数组中第0列的最小值为24,并且与第4行相关联(然后,我需要在
newArray [1] [0]
中分配4) -
然后,数组中第0列的最小值为124,并且与第0行相关联(然后,我需要在
newArray [2] [0]
中分配0) -
每列等等...
该方法的最终输出必须类似于以下2d数组.
任何帮助将不胜感激.
如果我正确理解:
IN:{{124,188,24,254,339},{0,7,77,145,159},{206,340,280,523,433},{310,265,151,411,398},{24,104,0,183,198}}出去 :{{1,1,4,4,1,1}{4,4,0,4,4}{0,0,1,0,0}{2,3,3,3,3}{3,2,2,2,2}
这是代码:
public static int [] [] createArray(int [] [] a){int [] [] nA =新的int [a.length] [a [0] .length];int [] col =新的int [a.length];int minIndex = -1;for(int i = 0; i< a.length; i ++){//首先将col输出for(int j = 0; j< a [0] .length; j ++){col [j] = a [j] [i];}//遍历colfor(int k = 0; k
也许有一种更简单的方法,但是它可以工作!
I need to build a method in Java where the input is a 2D array of integers and get as a result a 2D array of integers where each element makes reference to a position of an element in a column. Let me explain that with an example. Consider as an input for the method a 2D arrays of 5x5 as follow:
int[][] array = new int[][]{
{124, 188, 24, 254, 339},
{0, 7, 77, 145, 159},
{206, 340, 280, 523, 433},
{310, 265, 151, 411, 398},
{24, 104, 0, 183, 198}};
Now I need to build a new 2D array of integer (I will call newArray
in the following) according to:
The minimum value of column 0 in the array is 0 and is associated with row 1 (then, I need to assign a 1 in
newArray[0][0]
).Next, the minimum value of column 0 in the array is 24 and is associated with row 4 (then, I need to assign a 4 in
newArray[1][0]
).Then, the minimum value of column 0 in the array is 124 and is associated with row 0 (then, I need to assign a 0 in
newArray[2][0]
).And so on for each column...
The final output of the method must be something like the following 2d array.
Any help would be highly appreciated.
If I understood correctly :
IN :
{{124, 188, 24, 254, 339},
{0, 7, 77, 145, 159},
{206, 340, 280, 523, 433},
{310, 265, 151, 411, 398},
{24, 104, 0, 183, 198}}
OUT :
{{1, 1, 4, 1, 1}
{4, 4, 0, 4, 4}
{0, 0, 1, 0, 0}
{2, 3, 3, 3, 3}
{3, 2, 2, 2, 2}
Here's the code :
public static int[][] createArray(int[][] a) {
int[][] nA = new int[a.length][a[0].length];
int[] col = new int[a.length];
int minIndex = -1;
for (int i = 0; i < a.length; i++) {
// First get the col out
for (int j = 0; j < a[0].length; j++) {
col[j] = a[j][i];
}
// Loop through the col
for (int k = 0; k < a[0].length; k++) {
int min = Integer.MAX_VALUE;
// Loop through the remaining numbers of the col
for (int j = 0; j < col.length; j++) {
// Find the remaining lowest number
if (min > col[j]) {
min = col[j];
minIndex = j;
}
}
// Delete the number from the array
col[minIndex] = Integer.MAX_VALUE;
// Set this number in the final array
nA[k][i] = minIndex;
}
}
return nA;
}
There might be an easier way, but it works !
这篇关于按列对2D整数数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!