1. 程式人生 > >Java——集合框架(線性表)

Java——集合框架(線性表)

線性表

線性表——可儲存重複的元素,允許使用者指定儲存位置;使用者可通過下標訪問元素。 List方法圖:

AbstractList類提供對List介面的部分實現,AbstractSequentialList類擴充套件了AbstractList類,以提供對連結串列的支援。

陣列線性表ArrayList和連結串列類LinkedList

ArrayList 用陣列儲存元素,陣列是動態建立的;並將當前陣列中的所有元素都複製到新建陣列中。 ArrayList ——通過下標隨機訪問元素,除末尾處外,不能在其他位置插入,刪除則ArrayList 更高效率 ArrayList類繼承圖: 這裡寫圖片描述 ArrayList類中方法圖:

(自行動手實現體會每個方法的含義) 這裡寫圖片描述 這裡寫圖片描述

LinkedList

LinkedList在連結串列中儲存元素。LinkedList支援任意位置插入、刪除元素。 LinkedList類繼承圖: LinkedList方法圖: 這裡寫圖片描述 這裡寫圖片描述 示例:

public class ArrayListTest {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(7);
        list.add
(8); list.add(0,9); //指定位置插入元素 list.add(3,90); System.out.println(list); LinkedList<Object> linkedList = new LinkedList<>(list); linkedList.add(1,"red"); linkedList.removeLast(); linkedList.addFirst("green"); System.out.println
("Display the linked list forward:"); ListIterator<Object> listIterator = linkedList.listIterator(); //迭代器實現遍歷 while (listIterator.hasNext()){ System.out.print(listIterator.next() + " "); } System.out.println(); System.out.println("Display the linked list backward:"); listIterator = linkedList.listIterator(linkedList.size()); //倒遍歷 while (listIterator.hasPrevious()){ System.out.print(listIterator.previous() + " "); } } }

線性表和集合的靜態方法

線性表不支援有序儲存,Java集合框架在Collections類中提供用於對線性表進行排序的靜態方法。 示例:(Collections中的其他方法請自行實現體會)

public class CollectionsTest {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("red","green","blue","zero");
        Collections.sort(list); //升序排序
        System.out.println(list);

        Collections.sort(list,Collections.reverseOrder()); //降序排列
        System.out.println(list);

        List<Integer> integerList = Arrays.asList(2,4,5,8,12,4,6,90,56);
        //線上性表中查詢一個鍵值,前提是線性表必須以升序排列  鍵值不存線上性表中 返回:-(插入點+1)(-(insertion point+1))
        System.out.println("(1) Index:"+Collections.binarySearch(integerList,7));
        System.out.println("(2) Index:"+Collections.binarySearch(integerList,8));

        List<String> list2 = Arrays.asList("red","green","blue","zero");
        System.out.println("(3) Index:"+Collections.binarySearch(list2,"zero"));
        System.out.println("(4) Index:"+Collections.binarySearch(list2,"lemon"));

        //元素逆序排列
        List<String> stringList = Arrays.asList("yellow","red","green","blue");
        Collections.reverse(stringList);
        System.out.println(stringList);

        //shuffle(list)方法對線性表的元素進行隨機重新排序
        List<String> stringList1 = Arrays.asList("yellow","red","green","blue");
        Collections.shuffle(stringList1);
        System.out.println(stringList1);

        //shuffle(list,Random)方法以一個指定的Random物件對線性表中的元素隨機重新排序
        List<String> stringList2 = Arrays.asList("yellow","red","green","blue");
        List<String> stringList3 = Arrays.asList("yellow","red","green","blue");
        Collections.shuffle(stringList2,new Random(20));
        Collections.shuffle(stringList3,new Random(20));
        System.out.println(stringList2);
        System.out.println(stringList3);

        //使用copy(det,src)將源線性表的所有元素以同樣的下標複製到目標線性表中;目標線性表必須和源線性表等長
        List<String> stringList4 = Arrays.asList("yellow","red","green","blue");
        List<String> stringList5 = Arrays.asList("while","do");
        Collections.copy(stringList4,stringList5);  //copy執行淺複製,複製的只是線性表中元素的引用
        System.out.println(stringList4);

        /*使用方法nCopies(int n,Object o)建立一個包含指定物件的n個副本的不可變線性表
        * nCopies方法建立的線性表不可變,因此不能在該線性表中新增,刪除,更新元素,所有的元素都有相同的引用
        * */
        List<GregorianCalendar> list1 = Collections.nCopies(5,new GregorianCalendar(2018,0,1));

        /*使用方法fill(List list,Object o) 用指定元素替換線性表中的所有元素*/
        List<String> string = Arrays.asList("yellow","red","green","blue");
        Collections.fill(string,"black");
        System.out.println(string);

        /*使用max,min方法找集合中的最大元素和最小元素,集合中元素必須實現Comparable介面或Comparator介面可比較的*/
        List<String> str = Arrays.asList("yellow","red","green","blue");
        System.out.println(Collections.max(str));
        System.out.println(Collections.min(str));

        /*若兩個集合沒有相同的元素,方法disjoint(collection1,collection2)返回true*/
        Collection<String> collections = Arrays.asList("red","do");
        Collection<String> collection = Arrays.asList("red","blue");
        Collection<String> strings = Arrays.asList("pink","tan");
        System.out.println(Collections.disjoint(collection,collections));
        System.out.println(Collections.disjoint(collection,strings));

        /*方法frequency(collection,element)可找出集合中某元素的出現次數*/
        Collection<String> collection1 = Arrays.asList("red","red","do");
        System.out.println(Collections.frequency(collection1,"red"));
    }
}