有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用数组。排序是否按列对3×3数组进行排序?

因此,我允许用户输入一个3乘3的数组,然后代码应该获取该数组并根据列对整数进行排序。例如:
[15,87,37,
55,5,22,
30、12、40]

变成
[15,5,22,
30、12、37、
55、87、40]

这里是我的方法,它似乎不适用于列。而是按行排序

public static double[][] sortColumns(double[][] array)
{
double[][] sorted = array;
  for(int x = 0; x < 3; x++)
  {
     Arrays.sort(sorted[x]);
  } //end loops
  return sorted;
} //end sortRows

我对编码不是很熟悉,所以我不能100%理解一些人使用的比较器。就这样吧。如果有人能很好地帮助我解决这个问题,那就太好了。多谢各位


共 (5) 个答案

  1. # 1 楼答案

    假设用户总是输入一个3乘3的数组,当您获得用户输入时,只需以不同的方式存储数组,以便更容易排序。基于列而不是行存储矩阵。您可以这样做:

    Scanner scan = new Scanner(System.in);
    int[] col1 = new int[3];
    int[] col2 = new int[3];
    int[] col3 = new int[3];
    for (int i=0; i<3; i++){ //Store based on column not row
        col1[i] = scan.nextInt();
        col2[i] = scan.nextInt();
        col3[i] = scan.nextInt();
    }
    int[][] matrix = new int[3][3];
    matrix[0] = col1;
    matrix[1] = col2;
    matrix[2] = col3;
    for (int i=0; i<3; i++){  //Sort columns
        Arrays.sort(matrix[i]);
    }
    //The following code is used to print your array properly by rows instead of columns 
    for (int i=0; i<3; i++){
        for (int j=0; j<3; j++){
            System.out.print(matrix[j][i]+" ");
        }
        System.out.println();
    }
    

    按列排序后,您可以将矩阵转换回按行存储,以便更容易打印

    如果您想让用户设置矩阵的大小以使其具有动态性,可以执行以下操作:

    Scanner scan = new Scanner(System.in);
    int N = 3;  //Size of matrix, You can have user input this as well. 
    
    int[][] matrix = new int[N][N];
    for (int n=0; n<N; n++){  //Initialize Columns
        for (int column=0; column<N; column++){
            matrix[column][n] = scan.nextInt();  //Store based on column
        }
    }
    for (int i=0; i<N; i++){  //Sort columns
        Arrays.sort(matrix[i]);
    }
    //The following code is used to print your array properly by rows instead of columns 
    for (int i=0; i<N; i++){
        for (int j=0; j<N; j++){
            System.out.print(matrix[j][i]+" ");
        }
        System.out.println();
    }
    
  2. # 2 楼答案

    这里有一个适用于您的cas的解决方案。为了按列排序,我按列检索值,然后将这些值存储到数组列表中,并对其进行排序,然后将排序后的值存储回列中(反转循环)

    public static void main(String[] args) {
            int[][] x = new int[][]{{15,87,37},{55,5,22},{30,12,40}};
    
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    a.add(x[j][i]);
                }
                Collections.sort(a);
                for (int k = 0; k < 3; k++) {
                    x[k][i] = a.get(k);
                }
                a = new ArrayList<>();
            }
    
            //for loop for testing purpose
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                   System.out.print(x[i][j] + ",");
                }
                System.out.println("\n");
            }
         }
    

    15,5,22,
    30,12,37,
    55,87,40,

  3. # 3 楼答案

    先转置它,然后对组件数组进行排序,然后再转置回来,怎么样

  4. # 4 楼答案

    为什么不创建一个临时数组来将列转换为行,然后对单个行进行排序,并将排序后的行设置回原始数组呢

    比如:

    public static double[][] sortColumns(double[][] array)
    {
    double[][] sorted = new double[3][3];
      for(int x = 0; x < 3; x++)
      {
         double[] column = new double[3]
         for(y =0; y < 3; y++){
             column[y] = array[y][x]; //convert column to array
         }
         Arrays.sort(column);
         for(y = 0; y < 3; y++){
             sorted[y][x] = column[y]; //convert array back to column
         }
      } //end loops
      return sorted;
    } //end sortRows
    
  5. # 5 楼答案

    基本上,您要做的是按照每个其他数组的相同索引对每个数组进行排序,这不是简单地内置于Java中的东西。您必须转置数组。此解决方案允许将来对矩阵进行操作。基本上,这意味着:

    [row][column] => [column][row] 
    

    在这种形式下,数组可以按照您想要的方式一个接一个地排序,然后转换回原始形式以获得预期的结果

    您需要为此编写代码。或者,您可以寻找一个已经进行转置的库。有很多矩阵库,比如JAMA