排序/创建嵌套的arraylists

时间:2014-12-30 23:27:10

标签: java arrays arraylist

这是我到目前为止的代码,尽管数组的大小需要更改。我需要使用循环向数组添加值;要做到这一点,我想我需要使用嵌套的ArrayList。但是,我能否以与下面已经完成的方式相同的方式对ArrayList进行排序,还是有一种方法可以添加到这个数组中?

 public static void main(final String[] args) {

     String[][] data = new String[][] {
            new String[] { "Roy","3", "12" },
            new String[] { "Daniel","5", "22" },
            new String[] { "Joe","2", "3" },
            new String[] { "Ryan","4", "4" },
            new String[] { "Leroy","2", "5" },
            new String[] { "Smith","1", "6" },};



    Arrays.sort(data, new Comparator<String[]>() {
        @Override
        public int compare(String[] entry1, final String[] entry2) {
             String st1 = entry1[2];
             String st2 = entry2[2];
             int n1 = Integer.parseInt(st1);
             int n2 = Integer.parseInt(st2);
             return Integer.compare(n2, n1);

        }
    });




    for ( String[] s : data) {
        System.out.println(s[0] + "            " + s[1]+ "           " + s[2]);
    }

}

1 个答案:

答案 0 :(得分:0)

是的,似乎没有问题。将String[][]的类型更改为data(数组 list 或字符串数​​组),而不是Arraylist<String[]>(字符串数组的数组)。所以这看起来像:

public static void main(final String[] args) {

     ArrayList<String[]> data = new ArrayList<String[]>();
     data.add(new String[] { "Roy","3", "12" });
     data.add(new String[] { "Daniel","5", "22" });
     data.add(new String[] { "Joe","2", "3" });
     data.add(new String[] { "Ryan","4", "4" });
     data.add(new String[] { "Leroy","2", "5" });
     data.add(new String[] { "Smith","1", "6" });

     Collections.sort(data, new Comparator<String[]>() {
        @Override
        public int compare(String[] entry1, final String[] entry2) {
             String st1 = entry1[2];
             String st2 = entry2[2];
             int n1 = Integer.parseInt(st1);
             int n2 = Integer.parseInt(st2);
             return Integer.compare(n2, n1);
        }
    });

    //Other stuff...
}

也许你的意思是你希望内部字符串数组是动态大小的。这也没有问题,它完全按照你的想法嵌套。

public static void main(final String[] args) {

     String[][] rawData = new String[][] {
        new String[] { "Roy","3", "12" },
        new String[] { "Daniel","5", "22" },
        new String[] { "Joe","2", "3" },
        new String[] { "Ryan","4", "4" },
        new String[] { "Leroy","2", "5" },
        new String[] { "Smith","1", "6" }};

     ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
     for(String[] arr : rawData){
         ArrayList<String> a = new ArrayList<String>();
         data.add(a);
         for(String s : arr){
             a.add(s);
         }
     }

     Collections.sort(data, new Comparator<ArrayList<String>>() {
        @Override
        public int compare(ArrayList<String> entry1, ArrayList<String> entry2) {
             String st1 = entry1.get(2);
             String st2 = entry2.get(2);
             int n1 = Integer.parseInt(st1);
             int n2 = Integer.parseInt(st2);
             return Integer.compare(n2, n1);
        }
    });

    //Other stuff...
}

在这两种情况下,您都应该注意比较器访问索引2。如果数组(或arraylist)的长度仅为1,会发生什么?在这种情况下,您希望比较器做什么?它们是有希望整数但可能不是整数的字符串列表。如果比较器试图将12与“hello”进行比较,你希望比较器做什么?