标签:swap wap class array object max tco 等于 arrays
数组有工具类 Arrays,集合同样有可操作 Collection 和 Map 的工具类:Collections
// Collections 工具类可操作的对象:Collection 和 Mappublic class TestCollections {public static void main(String[] args) {List list = new ArrayList();list.add("s");list.add("b");list.add("j");list.add("n");list.add("a");System.out.println(list);// [s, b, j, n, a]// -----------------关于排序-----------------// 1.反转指定列表中元素的顺序:void reverse(List list);Collections.reverse(list);System.out.println(list);// [a, n, j, b, s]// 2.使用默认随机源对指定列表进行置换:void shuffle(List list);// 3.根据元素的自然顺序对指定列表按升序进行排序,列表中的所有元素都必须实现 Comparable// 接口。此外,列表中的所有元素都必须是可相互比较的:void sort(List list);Collections.sort(list);System.out.println(list);// [a, b, j, n, s]// 4.根据指定比较器产生的顺序对指定列表进行排序,此列表内的所有元素都必须可使用指定比较器相互比较:// void sort(List list,Comparator c);// 参见 TreeSet 的定制排序// 5.在指定列表的指定位置处交换元素:void swap(List list,int i,int j);Collections.swap(list, 0, 2);System.out.println(list);// [j, b, a, n, s]// -----------------关于查找、替换-----------------// 1.根据元素的自然顺序,返回给定 collection 的最大元素:T max(Collection coll);System.out.println(Collections.max(list));// s// 2.根据指定比较器产生的顺序,返回给定 collection 的最大元素:// T max(Collection coll,Comparator comp);// 3.返回指定 collection 中等于指定对象的元素数:int frequency(Collection c,Object o);list.add("a");System.out.println(list);// [j, b, a, n, s, a]System.out.println(Collections.frequency(list, "a"));// 2// 4.将所有元素从一个列表复制到另一个列表:void copy(List dest,List src);// 注意:目标列表(dest.size)的长度至少必须等于源列表(src.size)List destList = Arrays.asList(new Object[list.size()]);// 或者大于list.size()Collections.copy(destList, list);System.out.println(destList);// [j, b, a, n, s, a]// 5.使用另一个值(newVal)替换列表中出现的所有某一指定值(oldVal):// boolean replaceAll(List list,T oldVal,T newVal);System.out.println(Collections.replaceAll(destList, "a", "o"));// trueSystem.out.println(destList);// [j, b, o, n, s, o]// -----------------关于同步控制-----------------// 举例(详见API):返回指定列表支持的同步(线程安全的)列表:List synchronizedList(List list);List synList = Collections.synchronizedList(list);}}
标签:swap wap class array object max tco 等于 arrays
原文地址:http://www.cnblogs.com/chendifan/p/6535709.html