现在的位置: 首页 > 综合 > 正文

【DataStructure】Descriptioin and usage of List

2017年12月10日 ⁄ 综合 ⁄ 共 2450字 ⁄ 字号 评论关闭

Statements: This blog was written by me, but most of content  is quoted from book【Data Structure with Java Hubbard】 

【Description】

Alistis a collection of elements that are accessible sequentially: the first element, followed by the second element, followed by the third element, and so on.
This is called sequential accessor linked access(as opposed to direct or indexed access)

【Interface】

From the JCF inheritance hierarchy , you can see that the Queue, Deque, and Setinterfaces all extend the Listinterface. Consequently, all of the List,Queue, Deque, and Setclasses implement the Listinterface. This includes the concrete classes: ArrayList, Vector, LinkedList, PriorityQueue, ArrayDeque,EnumSet,HashSet,LinkedHashSet,and TreeSet.the JCF provides both linked and indexed implementations of the Listinterface: the LinkedList
class uses sequential access, while the ArrayListclass provides direct access.

【Demo】

This demo is related to subList.
package com.albertshao.ds.list;
//  Data Structures with Java, Second Edition
//  by John R. Hubbard
//  Copyright 2007 by McGraw-Hill

import java.util.*;

public class TestSubList {
  public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    Collections.addAll(list, "A","B","C","D","E","F","G","H","I","J");
    System.out.println(list);
    System.out.println("list.subList(3,8): " + list.subList(3,8));
    System.out.println("list.subList(3,8).get(2): "
        + list.subList(3,8).get(2));
    System.out.println("list.subList(3,8).set(2,\"B\"):");
    list.subList(3,8).set(2, "B");
    System.out.println(list);
    System.out.println("list.indexOf(\"B\"): " + list.indexOf("B"));
    System.out.println("list.subList(3,8).indexOf(\"B\"): "
        + list.subList(3,8).indexOf("B"));
    System.out.println(list);
    System.out.println("Collections.reverse(list.subList(3,8)):");
    Collections.reverse(list.subList(3,8));
    System.out.println(list);
    System.out.println("Collections.rotate(list.subList(3,8), 2):");
    Collections.rotate(list.subList(3,8), 2);
    System.out.println(list);
    System.out.println("Collections.fill(list.subList(3,8), \"X\"):");
    Collections.fill(list.subList(3,8), "X");
    System.out.println(list);
    list.subList(3,8).clear();
    System.out.println(list);
  }
}

【Result】

[A, B, C, D, E, F, G, H, I, J]
list.subList(3,8): [D, E, F, G, H]
list.subList(3,8).get(2): F
list.subList(3,8).set(2,"B"):
[A, B, C, D, E, B, G, H, I, J]
list.indexOf("B"): 1
list.subList(3,8).indexOf("B"): 2
[A, B, C, D, E, B, G, H, I, J]
Collections.reverse(list.subList(3,8)):
[A, B, C, H, G, B, E, D, I, J]
Collections.rotate(list.subList(3,8), 2):
[A, B, C, E, D, H, G, B, I, J]
Collections.fill(list.subList(3,8), "X"):
[A, B, C, X, X, X, X, X, I, J]
[A, B, C, I, J]

抱歉!评论已关闭.