List、Collections

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 stub
		List<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"));
		//定义一个新的List
		List<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]
*/


     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值