Collections 包装类常用方法

1.copy(List m,List n)方法的使用:(含义:将集合n中的元素全部复制到m中,并且覆盖相应索引的元素)。

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class AA {
	public static void main(String[] args) throws IOException {
		//copy(List m,List n)方法的使用:
        //(含义:将集合n中的元素全部复制到m中,并且覆盖相应索引的元素)。
		List mList = Arrays.asList("one two three four five six siven".split(" "));
		System.out.println(mList);
		List nList = Arrays.asList("我 是 复制的".split(" "));
		System.out.println(nList);
		Collections.copy(mList, nList);
		System.out.println(mList); 
	}

}

 2.sort(list)方法的使用(含义:对集合进行排序)。

public static void main(String[] args) throws IOException {
		
		List mList = Arrays.asList("j a v a e e ".split(" "));
		Collections.sort(mList);
		System.out.println(mList);
}	

 3.shuffle(Collection)方法的使用(含义:对集合进行随机排序)。

public static void main(String[] args) throws IOException {
		List mList = Arrays.asList("j a v a e e ".split(" "));
		Collections.shuffle(mList);
		System.out.println(mList);
		Collections.shuffle(mList);
		System.out.println(mList);
}	
	

 

 4.reverse()方法的使用(含义:反转集合中元素的顺序)。

public static void main(String[] args) throws IOException {
		List mList = Arrays.asList("j a v a e e ".split(" "));
		System.out.println(mList);
		Collections.reverse(mList);
		System.out.println(mList);
}	

 5.binarySearch(Collection,Object)方法的使用(含义:查找指定集合中的元素,返回所查找元素的索引)


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class AA{
	
	public static void main(String[] args) throws IOException {
		//binarySearch(Collection,Object)方法的使用(含义:查找指定集合中的元素,返回所查找元素的索引)
		List mList = Arrays.asList("j a v a e e k".split(" "));
		System.out.println(mList);
		int m = Collections.binarySearch(mList, "a");
        System.out.println(m);
        int n = Collections.binarySearch(mList, "e");
        System.out.println(n);
        int t = Collections.binarySearch(mList, "k");
        System.out.println(t);
		
	
		  List c = new ArrayList();
          c.add("j");
          c.add("a");
          c.add("v");
          c.add("a");
          c.add("e");
          c.add("e");
          c.add("k");
          System.out.println(c);
          int m1 = Collections.binarySearch(c, "a");
          System.out.println(m1);
          int n1 = Collections.binarySearch(c, "e");
          System.out.println(n1);
          int t1 = Collections.binarySearch(c, "k");
          System.out.println(t1);
	}	
	
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值