【DataStructure】Descriptioin and usage of List

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]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值