【问题标题】:Java - Uncommon sort a multidimensional arrayJava - 不常见的对多维数组进行排序
【发布时间】:2014-07-07 13:08:21
【问题描述】:

我需要这个练习的帮助

假设你有二维数组

String[][]myArray={
        {"0.0","0.1","0.2","0.3","0.4"},
        {"1.0","1.1","1.2","1.3"},
        {"2.0","2.1","2.2","2.3","2.4","2.5"}
        };

并且您希望打印:

0.0|1.0|2.0|
0.1|1.1|2.1|
0.2|1.2|2.1|
0.3|1.3|2.3|
0.4|2.4|
2.5|

我尝试过使用比较器

这里是代码:

 public static void main(String[] args) {

    String[][]Array={
            {"0.0","0.1","0.2","0.3","0.4"},
            {"1.0","1.1","1.2","1.3"},
            {"2.0","2.1","2.2","2.3","2.4","2.5"}
            };

    Arrays.sort(Array, new PorKolumn(2));

    for (int i = 0; i < Array.length; i++) {

        String [] kol = Array[i];
        for(int j=0; j<kol.length; j++) {
            System.out.print(kol[j] + " | ");

        }
        System.out.println();
    }
    }


}

class PorKolumn implements Comparator{
    int Kolumny;

    public PorKolumn(int Kolumny) {
        this.Kolumny = Kolumny;

    }
    public int compare (Object o1, Object o2 ){
        String [] kol1 = (String []) o1;
        String [] kol2 = (String []) o2;

        return kol1[Kolumny].compareTo(kol2[Kolumny]);

    }'

感谢任何帮助。

【问题讨论】:

  • 所以你想对每个数组进行排序,然后根据排序后的第一个值对数组进行排序对吗?
  • 你只想垂直打印内部数组吗??
  • 我没有看到任何排序。我看到您垂直打印内部数组,然后将所有内容尽可能向左推。
  • 请描述您需要的确切排序顺序。一个例子是不够的......
  • 是的,确实没有排序方法,我忘记粘贴了。你的回答给我指明了道路。谢谢大家。真的很棒的地方和人:)。下次我会更加小心。再次感谢。

标签: java arrays algorithm multidimensional-array


【解决方案1】:
public static void print( String[][] a ){
    int iRow = 0;
    while(true){
        boolean done = false;
        for( int iCol = 0; iCol < a.length; iCol++ ){
             if( iRow < a[iCol].length ){
                System.out.print( a[iCol][iRow] + "|" );
                done = true;
             }
        }
        if( ! done ) break;
        System.out.println();
        iRow++;
    }
}

按要求打印:

0.0|1.0|2.0|
0.1|1.1|2.1|
0.2|1.2|2.2|
0.3|1.3|2.3|
0.4|2.4|
2.5|

【讨论】:

    【解决方案2】:

    正如@Teepeemm 所说,没有排序:

    String[][]myArray={
            {"0.0","0.1","0.2","0.3","0.4"},
            {"1.0","1.1","1.2","1.3"},
            {"2.0","2.1","2.2","2.3","2.4","2.5"}
            };
    
        int maxLength = 0;
        for(int i = 0; i < myArray.length; i++) 
            if(maxLength < myArray[i].length)
                maxLength = myArray[i].length;
    
        for(int j = 0; j < maxLength; j++) {
            for(int i = 0; i < myArray.length; i++) 
                if(j < myArray[i].length) 
                    System.out.print(myArray[i][j] + "|");
            System.out.print("\n");
            }
        }
    

    输出:

    0.0|1.0|2.0|
    0.1|1.1|2.1|
    0.2|1.2|2.2|
    0.3|1.3|2.3|
    0.4|2.4|
    2.5|
    

    【讨论】:

      【解决方案3】:

      你可以试试这样的: (这是未经测试的代码,因为我这里是直接写的)

      int i = 0;
      boolean ongoing = true;
      
      while(ongoing){
      
        for(int j = 0; j < myArray.length; j++){
          if(myArray[j][i] == null){
            System.out.print("\t|");
            ongoing = false;
            continiue;
          }
          ongoing = true;
          System.out.print(myArray[j][i]+"\t|");
        }  
        System.out.print("\n");
        i++;
      }
      

      它应该给出正确的格式。

      【讨论】:

        猜你喜欢
        • 2012-04-22
        • 2010-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        相关资源
        最近更新 更多