使用Lists

List(接口) 顺序是List最重要的特性;它可保证元素按照规定的顺序排列。List为Collection添加了大量方法,以便我们在List中部插入和删除元素(只推荐对LinkedList这样做)。List也会生成一个ListIterator(列表反复器),利用它可在一个列表里朝两个方向遍历,同时插入和删除位于列表中部的元素(同样地,只建议对LinkedList这样做)
ArrayList* 由一个数组后推得到的List。作为一个常规用途的对象容器使用,用于替换原先的Vector。允许我们快速访问元素,但在从列表中部插入和删除元素时,速度却嫌稍慢。一般只应该用ListIterator对一个ArrayList进行向前和向后遍历,不要用它删除和插入元素;与LinkedList相比,它的效率要低许多
LinkedList 提供优化的顺序访问性能,同时可以高效率地在列表中部进行插入和删除操作。但在进行随机访问时,速度却相当慢,此时应换用ArrayList。也提供了addFirst(),addLast(),getFirst(),getLast(),removeFirst()以及removeLast()(未在任何接口或基础类中定义),以便将其作为一个规格、队列以及一个双向队列使用。


List (interface)
 


Order is the most important feature of a List; it promises to maintain elements in a particular sequence. List adds a number of methods to Collection that allow insertion and removal of elements in the middle of a List. (This is recommended only for a LinkedList.) A List will produce a ListIterator, and using this you can traverse the List in both directions, as well as insert and remove elements in the middle of the list (again, recommended only for a LinkedList).
 


ArrayList*
 


A List backed by an array. Use instead of Vector as a general-purpose object holder. Allows rapid random access to elements, but is slow when inserting and removing elements from the middle of a list. ListIterator should be used only for back-and-forth traversal of an ArrayList, but not for inserting and removing elements, which is expensive compared to LinkedList.
 


LinkedList
 


Provides optimal sequential access, with inexpensive insertions and deletions from the middle of the list. Relatively slow for random access. (Use ArrayList instead.) Also has addFirst(), addLast(), getFirst(), getLast(), removeFirst(), and removeLast() (which are not defined in any interfaces or base classes) to allow it to be used as a stack, a queue, and a dequeue.
 

下面这个例子中的方法每个都覆盖了一组不同的行为:每个列表都能做的事情(basicTest()),通过一个反复器遍历(iterMotion())、用一个反复器改变某些东西(iterManipulation())、体验列表处理的效果(testVisual())以及只有LinkedList才能做的事情等:

//: List1.java
// Things you can do with Lists
package c08.newcollections;
import java.util.*;

public class List1 {
  // Wrap Collection1.fill() for convenience:
  public static List fill(List a) {
    return (List)Collection1.fill(a);
  }
  // You can use an Iterator, just as with a
  // Collection, but you can also use random
  // access with get():
  public static void print(List a) {
    for(int i = 0; i < a.size(); i++)
      System.out.print(a.get(i) + " ");
    System.out.println();
  }
  static boolean b;
  static Object o;
  static int i;
  static Iterator it;
  static ListIterator lit;
  public static void basicTest(List a) {
    a.add(1, "x"); // Add at location 1
    a.add("x"); // Add at end
    // Add a collection:
    a.addAll(fill(new ArrayList()));
    // Add a collection starting at location 3:
    a.addAll(3, fill(new ArrayList()));
    b = a.contains("1"); // Is it in there
    // Is the entire collection in there
    b = a.containsAll(fill(new ArrayList()));
    // Lists allow random access, which is cheap
    // for ArrayList, expensive for LinkedList:
    o = a.get(1); // Get object at location 1
    i = a.indexOf("1"); // Tell index of object
    // indexOf, starting search at location 2:
    i = a.indexOf("1", 2);
    b = a.isEmpty(); // Any elements inside
    it = a.iterator(); // Ordinary Iterator
    lit = a.listIterator(); // ListIterator
    lit = a.listIterator(3); // Start at loc 3
    i = a.lastIndexOf("1"); // Last match
    i = a.lastIndexOf("1", 2); // ...after loc 2
    a.remove(1); // Remove location 1
    a.remove("3"); // Remove this object
    a.set(1, "y"); // Set location 1 to "y"
    // Keep everything that's in the argument
    // (the intersection of the two sets):
    a.retainAll(fill(new ArrayList()));
    // Remove elements in this range:
    a.removeRange(0, 2);
    // Remove everything that's in the argument:
    a.removeAll(fill(new ArrayList()));
    i = a.size(); // How big is it
    a.clear(); // Remove all elements
  }
  public static void iterMotion(List a) {
    ListIterator it = a.listIterator();
    b = it.hasNext();
    b = it.hasPrevious();
    o = it.next();
    i = it.nextIndex();
    o = it.previous();
    i = it.previousIndex();
  }
  public static void iterManipulation(List a) {
    ListIterator it = a.listIterator();
    it.add("47");
    // Must move to an element after add():
    it.next();
    // Remove the element that was just produced:
    it.remove();
    // Must move to an element after remove():
    it.next();
    // Change the element that was just produced:
    it.set("47");
  }
  public static void testVisual(List a) {
    print(a);
    List b = new ArrayList();
    fill(b);
    System.out.print("b = ");
    print(b);
    a.addAll(b);
    a.addAll(fill(new ArrayList()));
    print(a);
    // Shrink the list by removing all the
    // elements beyond the first 1/2 of the list
    System.out.println(a.size());
    System.out.println(a.size()/2);
    a.removeRange(a.size()/2, a.size()/2 + 2);
    print(a);
    // Insert, remove, and replace elements
    // using a ListIterator:
    ListIterator x = a.listIterator(a.size()/2);
    x.add("one");
    print(a);
    System.out.println(x.next());
    x.remove();
    System.out.println(x.next());
    x.set("47");
    print(a);
    // Traverse the list backwards:
    x = a.listIterator(a.size());
    while(x.hasPrevious())
      System.out.print(x.previous() + " ");
    System.out.println();
    System.out.println("testVisual finished");
  }
  // There are some things that only
  // LinkedLists can do:
  public static void testLinkedList() {
    LinkedList ll = new LinkedList();
    Collection1.fill(ll, 5);
    print(ll);
    // Treat it like a stack, pushing:
    ll.addFirst("one");
    ll.addFirst("two");
    print(ll);
    // Like "peeking" at the top of a stack:
    System.out.println(ll.getFirst());
    // Like popping a stack:
    System.out.println(ll.removeFirst());
    System.out.println(ll.removeFirst());
    // Treat it like a queue, pulling elements
    // off the tail end:
    System.out.println(ll.removeLast());
    // With the above operations, it's a dequeue!
    print(ll);
  }
  public static void main(String args[]) {
    // Make and fill a new list each time:
    basicTest(fill(new LinkedList()));
    basicTest(fill(new ArrayList()));
    iterMotion(fill(new LinkedList()));
    iterMotion(fill(new ArrayList()));
    iterManipulation(fill(new LinkedList()));
    iterManipulation(fill(new ArrayList()));
    testVisual(fill(new LinkedList()));
    testLinkedList();
  }
} ///:~

在basicTest()和iterMotiion()中,只是简单地发出调用,以便揭示出正确的语法。而且尽管捕获了返回值,但是并未使用它。在某些情况下,之所以不捕获返回值,是由于它们没有什么特别的用处。在正式使用它们前,应仔细研究一下自己的联机文档,掌握这些方法完整、正确的用法。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索arraylist
, list
, linkedlist
, system
, fill
, listiterator
, The
addAll
cmakelists.txt 使用、lists transform 使用、cmakelists.txt 详解、lists.newarraylist、cmakelists.txt,以便于您获取更多的相关知识。

时间: 2025-01-02 04:23:12

使用Lists的相关文章

十四 定义列表 Definition Lists

十四 定义列表 Definition Lists前面讲述了无序列表和有序列表,但一些像Peter Cushing's Doctor,经常忘记定义列表.这也许是因为它们和无序列表.有序列表比起来比较特殊而且没有用处.但一些列表条件和表述(像术语表),就必须使用定义列表. dl元素像ul和ol元素,建立列表,不过比它们两个多了一个dt元素用来definition term定义条件,接下来的dd定义表述definition description. 一个dt不仅仅只跟一个dd,互相之间可以有好多个.例

六 列表 lists

六 列表 lists 列表分为三种:无序列表(unordered lists), 有序列表(ordered lists) and 定义列表(definition lists),这里我先讲前两个,第三个在后面叙述. 无序列表和有序列表的作用基本相同,除了前者是用来表示非连续的列表,列表前面通常是黑点.后者是连续列表,列表前面的数字. ul标签用来定义无序列表,ol标签定义有序列表.li标签定义每个列表目录. 粘贴下面代码 <!DOCTYPE html PUBLIC "-//W3C//DTD

《基于MFC的OpenGL编程》Part 12 Creating and Using Display Lists

本文对第11篇文章进行修改,使用显示列表来存储渲染命令. 显示列表 OpenGL provides a facility to create a preprocessed set of OpenGL commands called a display list. Creating a display list is a straight forward process. We just have to delimit the display list code with glNewList an

shared pool系列二:free lists/shared pool lru list

介绍free lists及shared pool lru list. Shared pool中chunk的分配 1.shared pool中的chunk的大小是不一样的,但是是连续的 2.因为chunk是分配的最小单元,因此session需要给对象分配空间的时候,会以chunk为单位进行申请 3.可用的chunk(free)会形成一个链表 feee lists,便于进行分配的时候,可以通过遍历链表寻找到可用的适合的chunk,链表是chunk进行组织和管理的一种方式 4.一个可用的chunk链表

再谈“在STL列表(Lists)中插入不同类型的对象”

看到贵网站上的一篇文章:"在STL列表(Lists)中插入不同类型的对象".我觉得该文回答还没有指出问题的本质,所以本人提出我的观点,恳请指正.本人认为,错误产生的原因在于指针转化过程中,程序没有指出该指针最初的原型,或者说,由于未找到正确的子类函数地址才发生调用错误的,本人原代码如下:用Dev-cpp的g++编译通过. #include<iostream> #include<algorithm> #include <vector> #include

使用WSS的Lists.UpdateListItems()方法之被截断的CAML

Microsoft的WSS(Windows Sharepoint Services)公开了很多用于访问和管理Sharepoint站点的方法 ,在调用这些方法时可以通过CAML(Collaborative Application Markup Language)进行一些操作.其 中Lists.UpdateListItems()方法提供了用于向Sharepoint List增.删.改数据的方法,但是需要通过 CAML语句告诉Sharepoint如何更新数据,有关如何使用CAML以及如何编写CAML进

STL源码学习——Lists(链表)

STL源码学习--Lists(链表) 今天突然想起来看看开源项目,找了找最后决定好好看看经典的STL喵~ 和STL里的代码比起来我突然觉得以前写的代码也太不规范了喵,估计很多ACMer都一样吧喵. 先从简单的看.先挑了一发list的源码来看.总结如下: 欢迎大家一起讨论喵~ 1 :list是用双向循环链表实现的,就是说 list.end()+1 == list.begin() 2 :list中有一个关键结点,这个结点是 list.end() 3 :在看了list中的erase函数后,发现这个函数

Coursera Scala 5-5 List:Reduction of Lists

Coursera Scala 5-5 List:Reduction of Lists 另一个list常用的操作是:连结集合元素.例如sum(list) ReduceLeft 使用ReduceLeft可以很方便的编写通用的连结方法 def sum(xs: List[Int]) = (0 :: xs) reduceLeft ((x,y) => x+y) def product(xs : List[Int]) = (1 :: xs) reduceLeft((x,y) => x*y) FoldLeft

为什么lists.add(examstudent)这行代码的lists始终为空

问题描述 为什么lists.add(examstudent)这行代码的lists始终为空 为啥会出现Null pointer access: The variable lists can only be null at this location错误 String sql = "select flow_id, type, id_card, exam_card, student_name, location, grade from examstudent"; PreparedStatem