排序整数ArrayList的一个ArrayList [英] sort an arraylist of arraylist of integers

查看:130
本文介绍了排序整数ArrayList的一个ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待排序整数ArrayList的一个ArrayList,我需要帮助吗?

我被告知,我需要实现比较器或类似的,然后使用collection.sort以列表的列表进行排序...

 的ArrayList< ArrayList的<整数GT;> G =新的ArrayList< ArrayList的<整数GT;>()如果你看一下表,如下例列表:
C1 - 5,4,10
C2 - 3,2,1
C3 - 7,8,6
首先将被排序如下:
C1 - 4,5,10
C2 - 1,2,3
C3 - 6,7,8
然后,它会被排序这样
C1 - 1,2,3
C2 - 4,5,6
C3 - 7,8,10


解决方案

没有对空列出了各种错误检查,但在这里。

 列表<名单,LT;整数GT;>列表= Arrays.asList(Arrays.asList(10,5,4),
        Arrays.asList(3,2,1),Arrays.asList(7,8,6));
对于(列表<整型→1:名单){
    Collections.sort(升);
}
Collections.sort(名单,新的比较<名单,LT;整数GT;>(){
    公众诠释比较(表<整数GT; 01,列表<整数GT; O2){
        返回o1.get(0).compareTo(o2.get(0));
    }
});
的System.out.println(名单);

在Java 8中会变得更加简洁:

 列表<名单,LT;整数GT;>列表= Arrays.asList(Arrays.asList(10,5,4),
                Arrays.asList(3,2,1),Arrays.asList(7,8,6));
list.forEach(收藏集::排序);
Collections.sort(列表(L1,L2) - > l1.get(0).compareTo(l2.get(0)));
的System.out.println(名单);

I am looking to sort an arraylist of arraylist of integers and I require help?

I was informed that I need to implement comparator or comparable and then use the collection.sort to sort the list of list in order...

ArrayList<ArrayList<Integer>> g = new ArrayList<ArrayList<Integer>>()

If you look at the list of list as the following example:
C1 – 5,4,10
C2 – 3,2,1
C3 – 7,8,6
First it will be sorted like this:
C1 – 4,5,10
C2 – 1,2,3
C3 – 6,7,8
Then it will be sorted like this
C1 – 1,2,3
C2 – 4,5,6
C3 – 7,8,10

解决方案

No error check for null lists, but here it is.

List<List<Integer>> list = Arrays.asList(Arrays.asList(10, 5, 4), 
        Arrays.asList(3, 2, 1), Arrays.asList(7, 8, 6));
for (List<Integer> l : list) {
    Collections.sort(l);
}
Collections.sort(list, new Comparator<List<Integer>>() {
    public int compare(List<Integer> o1, List<Integer> o2) {
        return o1.get(0).compareTo(o2.get(0));
    }
});
System.out.println(list);

With Java 8 it gets even more concise:

List<List<Integer>> list = Arrays.asList(Arrays.asList(10, 5, 4),
                Arrays.asList(3, 2, 1), Arrays.asList(7, 8, 6));
list.forEach(Collections::sort);
Collections.sort(list, (l1, l2) -> l1.get(0).compareTo(l2.get(0)));
System.out.println(list);

这篇关于排序整数ArrayList的一个ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆