- /*
- 集合框架的工具类
- Collections:
- */
- import java.util.*;
- class Test
- {
- public static void main(String [] args)
- {
- List<String> ls = new ArrayList<String>();
- ls.add("aaa");
- ls.add("afs");
- ls.add("fds");
- ls.add("bcd");
- sop(ls);
- Collections.sort(ls);
- sop(ls);
- String tmp = Collections.max(ls);
- sop("max :"+tmp);
- int index = Collections.binarySearch(ls,"bcd");
- sop("index :" + index);
- int index2 = Collections.binarySearch(ls,"bcde");
- sop("index2 :" + index2);
- //如果搜索键包含在列表中,则返回搜索键的索引;否则返回 (-(插入点) - 1)
- //替换
- Collections.replaceAll(ls, "afs","KK");
- sop(ls);
- //反转,逆置
- Collections.reverse(ls);
- sop(ls);
- //fill 方法可以将list集合中所有元素转换成指定元素
- Collections.fill(ls,"pp");
- sop(ls);
- }
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- }