9. java.util.Collections

来源:互联网 发布:js怎么隐藏标签 编辑:程序博客网 时间:2024/05/01 15:55

java.util.Collections 是 java.lang.Object的直接子类,它是一个工具类。内部方法都是静态方法,可以直接类名调用。

static <T> void     sort(List<T> list, Comparator<? super T> c)Sorts the specified list according to the order induced by the specified comparator.static void     reverse(List<?> list)Reverses the order of the elements in the specified list.static void     shuffle(List<?> list)Randomly permutes the specified list using a default source of randomness.static void     swap(List<?> list, int i, int j)Swaps the elements at the specified positions in the specified list.static <T> void     copy(List<? super T> dest, List<? extends T> src)Copies all of the elements from one list into another.static <T extends Object & Comparable<? super T>>   T   max(Collection<? extends T> coll)Returns the maximum element of the given collection, according to the natural ordering of its elements.static <T extends Object & Comparable<? super T>>   T   min(Collection<? extends T> coll)Returns the minimum element of the given collection, according to the natural ordering of its elements.static void     rotate(List<?> list, int distance)Rotates the elements in the specified list by the specified distance.static <T> boolean  replaceAll(List<T> list, T oldVal, T newVal)Replaces all occurrences of one specified value in a list with another.
import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;public class LianXi {    public static void main(String[] args) throws Exception {        t_1();    }    public static void t_1() {        int[] i = {3, 1, 4, 1, 5, 9, 2, 5};        List l = new ArrayList();        for (int j = 0; j < i.length; j++) {            l.add(i[j]);        }        for (int j = 0; j < i.length; j++) {            System.out.print(l.get(j) + " ");       // 3 1 4 1 5 9 2 5         }        Collections.sort(l);        // 正顺序        System.out.println();        for (int j = 0; j < i.length; j++) {            System.out.print(l.get(j) + " ");       // 1 1 2 3 4 5 5 9         }        Collections.reverse(l);     // 反顺序        System.out.println();        for (int j = 0; j < i.length; j++) {            System.out.print(l.get(j) + " ");       // 9 5 5 4 3 2 1 1         }        Collections.shuffle(l);     // 随机顺序,每次都不一样        System.out.println();        for (int j = 0; j < i.length; j++) {            System.out.print(l.get(j) + " ");       // 4 3 5 9 5 1 2 1         }        Collections.sort(l);        Collections.rotate(l, 0);        // 把列表中的元素轮换位置,0不变,正数是把后边的数移动到前边,负数是把前边的数移动到后边        System.out.println();        for (int j = 0; j < i.length; j++) {            System.out.print(l.get(j) + " ");       // 1 1 2 3 4 5 5 9         }        Collections.sort(l);        Collections.replaceAll(l, 1, 100);        // 把列表中所有的1转变为100        System.out.println();        for (int j = 0; j < i.length; j++) {            System.out.print(l.get(j) + " ");       // 100 100 2 3 4 5 5 9         }        System.out.println();        Collections.sort(l);        List ll = new ArrayList();        Collections.addAll(ll,  new Integer[l.size()]);        // 新建的列表大小默认为0,在复制之前须设定大小,不然报错        Collections.copy(ll, l);        for (int j = 0; j < i.length; j++) {            System.out.print(ll.get(j) + " ");      // 2 3 4 5 5 9 100 100 ll列表中有了相同的元素        }        Collections.sort(l);        Collections.swap(l, 2, 5);        // 交换列表中两个索引的位置,这两个数字位置互换效果一样。        System.out.println();        for (int j = 0; j < i.length; j++) {            System.out.print(l.get(j) + " ");       // 2 3 9 5 5 4 100 100         }        System.out.println();        System.out.println(Collections.min(l));     // 2        System.out.println(Collections.max(l));     // 100    }}
原创粉丝点击