collection与conllections

collection是java的集合,
conllections就像一个工具类,服务于Java的Collection框架。
http://www.cnblogs.com/guweiwei/p/6511974.html

collections常见的方法

sort(Collection)方法的使用(含义:对集合进行排序)。
例:对已知集合c进行排序?
              public class Practice {
                      public static void main(String[] args){
                                   List c = new ArrayList();
                                c.add("l");
                                c.add("o");
                               c.add("v");
                                c.add("e");
                              System.out.println(c);
                                Collections.sort(c);
                                System.out.println(c);
                        }
              }
        运行结果为:[l, o, v, e]
                  [e, l, o, v]

shuffle(Collection)方法的使用(含义:对集合进行随机排序)。
例:shuffle(Collection)的简单示例?
           public class Practice {
                     public static void main(String[] args){
                                  List c = new ArrayList();
                             c.add("l");
                              c.add("o");
                              c.add("v");
                             c.add("e");
                                 System.out.println(c);
                               Collections.shuffle(c);
                             System.out.println(c);
                              Collections.shuffle(c);
                               System.out.println(c);
                        }
             }
            运行结果为:[l, o, v, e]
                              [l, v, e, o]
                              [o, v, e, l]

copy(List m,List n)方法的使用(含义:将集合n中的元素全部复制到m中,并且覆盖相应索引的元素)。
例:
            public class Practice {
                    public static void main(String[] args){
                            List m = Arrays.asList("one two three four five six siven".split(" "));
                            System.out.println(m);
                             List n = Arrays.asList("我 是 复制过来的哈".split(" "));
                             System.out.println(n);
                             Collections.copy(m,n);
                                System.out.println(m);
                      }
             }
   运行结果为:[one, two, three, four, five, six, siven]
                         [我, 是, 复制过来的哈]
                        [我, 是, 复制过来的哈, four, five, six, siven]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容