栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Collections 包装类常用方法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
	}	
	
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004578.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号