按列对二维整数数组进行排序
Sorting 2D array of integers by column
我需要在 Java 中构建一个方法,其中输入是一个二维整数数组,并得到一个二维整数数组,其中每个元素都引用列中元素的位置.让我用一个例子来解释。将 5x5 的二维数组作为该方法的输入,如下所示:
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}};
现在我需要根据:
建立一个新的整数二维数组(我将在下面调用newArray
)
数组中第0列的最小值为0,与第1行相关联(那么,我需要在newArray[0][0]
中赋1)。
接下来,数组中第0列的最小值为24,关联到第4行(那么,我需要在newArray[1][0]
中赋值一个4)。
那么,数组中第0列的最小值为124,关联到第0行(那么,我需要在newArray[2][0]
中赋0)。
每一列依此类推...
该方法的最终输出必须类似于以下二维数组。
非常感谢任何帮助。
如果我没理解错的话:
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}
代码如下:
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;
}
可能有更简单的方法,但它确实有效!
按列对矩阵元素的索引进行排序是对转置矩阵的按行对元素的索引进行排序:
int m = 5;
int n = 6;
int[][] arr1 = new int[][]{
{124, 188, 24, 254, 339, 3},
{0, 7, 77, 145, 159, 1},
{206, 340, 280, 523, 433, 5},
{310, 265, 151, 411, 398, 4},
{24, 104, 0, 183, 198, 2}};
int[][] arr2 = IntStream
// iterate over the indices
// of the rows of the array
.range(0, n)
.mapToObj(i -> IntStream
// iterate over the
// indices of the columns
.range(0, m)
.boxed()
// sort indices of the elements of the
// columns by its values in the array
.sorted(Comparator.comparingInt(j -> arr1[j][i]))
.mapToInt(Integer::intValue)
// sorted column of indices is
// a row in the new array
.toArray())
// return sorted array of indices
.toArray(int[][]::new);
// transpose the array of indices
int[][] arr3 = new int[m][n];
IntStream.range(0, m).forEach(i ->
IntStream.range(0, n).forEach(j ->
arr3[i][j] = arr2[j][i]));
// output
Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);
输出:
[1, 1, 4, 1, 1, 1]
[4, 4, 0, 4, 4, 4]
[0, 0, 1, 0, 0, 0]
[2, 3, 3, 3, 3, 3]
[3, 2, 2, 2, 2, 2]
我需要在 Java 中构建一个方法,其中输入是一个二维整数数组,并得到一个二维整数数组,其中每个元素都引用列中元素的位置.让我用一个例子来解释。将 5x5 的二维数组作为该方法的输入,如下所示:
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}};
现在我需要根据:
建立一个新的整数二维数组(我将在下面调用newArray
)
数组中第0列的最小值为0,与第1行相关联(那么,我需要在
newArray[0][0]
中赋1)。接下来,数组中第0列的最小值为24,关联到第4行(那么,我需要在
newArray[1][0]
中赋值一个4)。那么,数组中第0列的最小值为124,关联到第0行(那么,我需要在
newArray[2][0]
中赋0)。每一列依此类推...
该方法的最终输出必须类似于以下二维数组。
非常感谢任何帮助。
如果我没理解错的话:
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}
代码如下:
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;
}
可能有更简单的方法,但它确实有效!
按列对矩阵元素的索引进行排序是对转置矩阵的按行对元素的索引进行排序:
int m = 5;
int n = 6;
int[][] arr1 = new int[][]{
{124, 188, 24, 254, 339, 3},
{0, 7, 77, 145, 159, 1},
{206, 340, 280, 523, 433, 5},
{310, 265, 151, 411, 398, 4},
{24, 104, 0, 183, 198, 2}};
int[][] arr2 = IntStream
// iterate over the indices
// of the rows of the array
.range(0, n)
.mapToObj(i -> IntStream
// iterate over the
// indices of the columns
.range(0, m)
.boxed()
// sort indices of the elements of the
// columns by its values in the array
.sorted(Comparator.comparingInt(j -> arr1[j][i]))
.mapToInt(Integer::intValue)
// sorted column of indices is
// a row in the new array
.toArray())
// return sorted array of indices
.toArray(int[][]::new);
// transpose the array of indices
int[][] arr3 = new int[m][n];
IntStream.range(0, m).forEach(i ->
IntStream.range(0, n).forEach(j ->
arr3[i][j] = arr2[j][i]));
// output
Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);
输出:
[1, 1, 4, 1, 1, 1]
[4, 4, 0, 4, 4, 4]
[0, 0, 1, 0, 0, 0]
[2, 3, 3, 3, 3, 3]
[3, 2, 2, 2, 2, 2]