Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_Multidimensional Array_Columnsorting - Fatal编程技术网

Java 按列对二维整数数组排序

Java 按列对二维整数数组排序,java,arrays,multidimensional-array,columnsorting,Java,Arrays,Multidimensional Array,Columnsorting,我需要在Java中构建一个方法,其中输入是一个二维整数数组,并得到一个二维整数数组,其中每个元素引用列中元素的位置。让我用一个例子来解释这一点。作为该方法的输入,5x5的2D阵列如下: int[][]数组=新的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}}; 现在,我需要根据

我需要在Java中构建一个方法,其中输入是一个二维整数数组,并得到一个二维整数数组,其中每个元素引用列中元素的位置。让我用一个例子来解释这一点。作为该方法的输入,5x5的2D阵列如下:

int[][]数组=新的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)

  • 接下来,数组中第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}}

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;
}
publicstaticint[]createArray(int[]a){
int[][]nA=新的int[a.length][a[0].length];
int[]col=新的int[a.长度];
int minIndex=-1;
for(int i=0;i列[j]){
min=col[j];
minIndex=j;
}
}
//从数组中删除该数字
col[minIndex]=整数的最大值;
//在最终数组中设置此数字
nA[k][i]=minIndex;
}
}
返回nA;
}

也许有一个更简单的方法,但它是有效的

如果我理解正确:

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;
}
publicstaticint[]createArray(int[]a){
int[][]nA=新的int[a.length][a[0].length];
int[]col=新的int[a.长度];
int minIndex=-1;
for(int i=0;i列[j]){
min=col[j];
minIndex=j;
}
}
//从数组中删除该数字
col[minIndex]=整数的最大值;
//在最终数组中设置此数字
nA[k][i]=minIndex;
}
}
返回nA;
}

也许有一个更简单的方法,但它是有效的

按列排序矩阵元素的索引是按转置矩阵的行排序元素的索引:

intm=5;
int n=6;
int[]arr1=新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
//迭代索引
//数组的行数
.范围(0,n)
.mapToObj(i->IntStream
//迭代
//列的索引
.范围(0,m)
.boxed()
//元素的排序索引
//按其在数组中的值排序的列
.sorted(Comparator.comparingit(j->arr1[j][i]))
.mapToInt(整数::intValue)
//索引的排序列为
//新数组中的一行
.toArray())
//返回已排序的索引数组
.toArray(int[]]::新建);
//转置索引数组
int[]arr3=新int[m][n];
IntStream.range(0,m).forEach(i->
IntStream.range(0,n).forEach(j->
arr3[i][j]=arr2[j][i]);
//输出
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]

按列对矩阵元素的索引进行排序就是按转置矩阵的行对元素的索引进行排序:

intm=5;
int n=6;
int[]arr1=新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
//迭代索引
//数组的行数
.范围(0,n)
.mapToObj(i->IntStream
//迭代
//列的索引
.范围(0,m)
.boxed()
//元素的排序索引
//按其在数组中的值排序的列
.sorted(Comparator.comparingit(j->arr1[j][i]))
.mapToInt(整数::intValue)
//索引的排序列为
//新数组中的一行
.toArray())
//返回已排序的索引数组
.toArray(int[]]::新建);
//转置i的数组