简体   繁体   English

Java:不按Arrays.sort()按列对2D数组排序

[英]Java: Sorting a 2D Array by Columns without Arrays.sort()

I'm trying to set up a method that will sort a two dimensional array of doubles by column. 我正在尝试建立一种方法,该方法将按列对double的二维数组进行排序。 Based on the provided specifications, this method is also not supposed to take ragged arrays with rows of unequal lengths. 根据提供的规范,此方法也不应采用行长不等的参差不齐的数组。 I'm testing this using the double[][] mdarray = {{3.0, 4.0, 1.0, 8.0}, {13.0, 2.0, 12.0, 9.0} 我正在使用double [] [] mdarray = {{3.0,4.0,1.0,8.0},{13.0,2.0,12.0,9.0}

Using a print method, this should be displayed as 使用打印方法时,应显示为

3.0, 2.0, 1.0, 8.0, 3.0、2.0、1.0、8.0,

13.0, 4.0, 12.0, 9.0, 13.0、4.0、12.0、9.0,

When using a separate print method to output the result, the array appears to be unchanged. 当使用单独的打印方法输出结果时,数组似乎保持不变。 I think my problem is figuring out how to check every row for a given column before moving onto the next, but I'm not sure how to do that easily without the Arrays.sort() method, which is not allowed for this exercise. 我认为我的问题是弄清楚如何在转到给定列之前检查给定列的每一行,但是我不确定如果没有Arrays.sort()方法,那么如何轻松地做到这一点,因此本练习不允许这样做。

public void sort(boolean byColumn)
{
    if(isRagged() == true)
    {
        System.out.println("Ragged arrays cannot be sorted by column.");
    }
    else
    {
        for(int i = 0; i < mdarray.length; i++)
        {
            for(int j = i + 1; j < mdarray.length - 1; j ++)
            {
                for(int k = 0; k < mdarray[i].length; k++)
                {
                    if(mdarray[i][k] > mdarray[j][k])
                    {
                        double temp = mdarray[i][k];
                        mdarray[i][k] = mdarray[j][k];
                        mdarray[i][k] = temp;
                    }
                }
            }
        }
    }
}

You've actually switched the bounds of i and j variables as well. 实际上,您也已经切换了ij变量的范围。 Since j iterates from i+1 , it should gain values up to mdarray.length - 1 , therefore the condition should be j < mdarray.length and similarly i 's upper bound should be mdarray.length - 2 therefore the condition should be i < mdarray.length - 1 . 由于ji+1迭代,因此它的值应高达mdarray.length - 1 ,因此条件应为j < mdarray.length ,类似地, i的上限应为mdarray.length - 2因此条件应为i < mdarray.length - 1

Oh and also there's the mistake we mentioned in comments - when switching the elements, assign the temp variable to mdarray[j][k] . 哦,还有我们在注释中提到的错误-切换元素时,将temp变量分配给mdarray[j][k]

As you know you can consider a 2D array as a group of columns or rows. 如您所知,您可以将2D数组视为一组列或行。 This program has to sort columns, so we'll consider the array as a group of columns. 该程序必须对列进行排序,因此我们将数组视为一组列。

The program is done: 该程序已完成:

  1. loops all columns 循环所有列
  2. choose your favorite algorithm for sorting a 1D array (take a look here ) 选择您最喜欢的算法来对一维数组进行排序( 在此处查看

     double[][] mdarray = {{3.0, 4.0, 1.0, 8.0}, {13.0, 2.0, 12.0, 9.0}}; //loop all columns for(int col = 0; col < mdarray[0].length; col++){ //loops all rows and use bubble sort algorithm in ascending order for(int i = 1; i < mdarray.length; i++){ for(int j = i; j < mdarray.length; j++){ if(mdarray[j - 1][col] > mdarray[j][col]){ //swap double temp = mdarray[j][col]; mdarray[j][col] = mdarray[j-1][col]; mdarray[j-1][col] = temp; } } } } 

If you print each row of mdarray , the output will be: 如果您打印mdarray每一行,输出将是:

mdarray[0]: 3.0  2.0 1.0  8.0
mdarray[1]: 13.0 4.0 12.0 9.0 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM