2015-11-19 67 views
2

所以,我允许用户输入一个3乘3的数组,然后代码应该接受数组并按列排序整数。如:
[15,87,37,
55,5,22,
30,12,40]

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

这是我的方法,似乎不适用于列。它是按行排序?如何使用Arrays.sort按列排序3乘3的数组?

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%的明白,我看到一些人用它代替的.sort这种比较。如果有人能够很好地帮助我解决这个很好的问题。谢谢。

+0

通过柱排序表明数据在逻辑上通过柱分组,不按行。所以最简单的方法是以列方式填充而不是按行方式(只有你的思路说第一个维度是行,它也可以是列)。你能告诉我们关于你存储的数据的一些信息吗? – Turing85

回答

0

假设用户总是输入一个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(); 
} 
+0

此解决方案不能缩放。 – Hypino

+0

@Hypino我没有按比例写它。我认为用户总是根据OP陈述的内容输入3乘3。 – gonzo

+0

@Hypino你可以很容易地扩展它。奇闻轶事所说的是“把它存放在专业而非专业”。这只是一种思维方式,第一个维度是“行”。 – Turing85

1

你如何转置它,然后排序组件数组,然后转置回来?

+0

这是什么意思?就像我说的我仍然是编程新手..你是说要做到这一点,所以我将列更改为有序数组中的行,然后按行排序,然后将其更改回来? – Nick

+0

是的,那是什么换位是。 –

1

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

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

在这种形式下,该阵列可以被分类一一在你想要的方式,然后转回到原来的形式给你预期的结果。

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

0

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

像:

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 
0

这里是会为你的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,

+0

你可以发布你指定的输入吗?我有一种感觉,这不是OP想要的(或者,至少不是我认为他想要的)。 – Turing85

+0

@ Turing85你怎么看? – Raf

+1

在仔细研究这个问题之后......这似乎是一个合法的解决方案。 – Turing85