JAVA Collections常用方法

  1. /* 
  2.     集合框架的工具类 
  3.  
  4.     Collections: 
  5. */  
  6.   
  7. import java.util.*;  
  8.   
  9. class Test  
  10. {  
  11.   
  12.     public static void main(String [] args)  
  13.     {  
  14.         List<String> ls = new ArrayList<String>();  
  15.           
  16.         ls.add("aaa");  
  17.         ls.add("afs");  
  18.         ls.add("fds");  
  19.         ls.add("bcd");  
  20.   
  21.         sop(ls);  
  22.   
  23.         Collections.sort(ls);  
  24.       
  25.         sop(ls);  
  26.   
  27.         String tmp = Collections.max(ls);  
  28.         sop("max :"+tmp);  
  29.   
  30.         int index = Collections.binarySearch(ls,"bcd");  
  31.         sop("index :" + index);  
  32.         int index2 = Collections.binarySearch(ls,"bcde");  
  33.         sop("index2 :" + index2);  
  34.         //如果搜索键包含在列表中,则返回搜索键的索引;否则返回 (-(插入点) - 1)  
  35.       
  36.         //替换   
  37.         Collections.replaceAll(ls, "afs","KK");  
  38.         sop(ls);  
  39.   
  40.         //反转,逆置  
  41.         Collections.reverse(ls);  
  42.         sop(ls);  
  43.   
  44.           
  45.         //fill 方法可以将list集合中所有元素转换成指定元素  
  46.         Collections.fill(ls,"pp");  
  47.         sop(ls);  
  48.     }  
  49.   
  50.     public static void sop(Object obj)  
  51.     {  
  52.         System.out.println(obj);  
  53.     }  
  54. }  

posted on 2017-04-07 14:14  老肖2017  阅读(702)  评论(0编辑  收藏  举报

导航