List、Collections

来源:互联网 发布:阿里云校园招聘 编辑:程序博客网 时间:2024/06/06 11:36

java.util.Collections提供了一些有关List操作的静态方法,很有效的对List进行操作

1.sort(List)---对List中的元素排序

2. shuffle(List)----对List中的元素随机排序

3.reverse(List)---对List中的元素反转

。。。。。。。。

import java.util.*;public class TestList {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubList<String> ls=new LinkedList<String>();for(int i=0;i<=9;i++){ls.add("a"+i);}System.out.println(ls);//随机排列Collections.shuffle(ls);System.out.println(ls);//逆序排列Collections.reverse(ls);System.out.println(ls);//排序Collections.sort(ls);System.out.println(ls);//二分查找System.out.println(Collections.binarySearch(ls, "a2"));//定义一个新的ListList<String> lst=new LinkedList<String>();for(int j=0;j<20;j++){lst.add("b"+j);}System.out.println(lst);Collections.copy(lst, ls);System.out.println(lst);}}//结果(不唯一)为:/*[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9][a4, a3, a6, a8, a1, a9, a7, a0, a2, a5][a5, a2, a0, a7, a9, a1, a8, a6, a3, a4][a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]2[b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19][a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19]*/


     

原创粉丝点击