
list(接口) 顺序是list最重要的特性;它可保证元素按照规定的顺序排列。list为collection添加了大量方法,以便我们在list中部插入和删除元素(只推荐对linkedlist这样做)。list也会生成一个listiterator(列表反复器),利用它可在一个列表里朝两个方向遍历,同时插入和删除位于列表中部的元素(同样地,只建议对linkedlist这样做)
arraylist* 由一个数组后推得到的list。作为一个常规用途的对象容器使用,用于替换原先的vector。允许我们快速访问元素,但在从列表中部插入和删除元素时,速度却嫌稍慢。一般只应该用listiterator对一个arraylist进行向前和向后遍历,不要用它删除和插入元素;与linkedlist相比,它的效率要低许多
linkedlist 提供优化的顺序访问性能,同时可以高效率地在列表中部进行插入和删除操作。但在进行随机访问时,速度却相当慢,此时应换用arraylist。也提供了addfirst(),addlast(),getfirst(),getlast(),removefirst()以及removelast()(未在任何接口或基础类中定义),以便将其作为一个规格、队列以及一个双向队列使用
下面这个例子中的方法每个都覆盖了一组不同的行为:每个列表都能做的事情(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()中,只是简单地发出调用,以便揭示出正确的语法。而且尽管捕获了返回值,但是并未使用它。在某些情况下,之所以不捕获返回值,是由于它们没有什么特别的用处。在正式使用它们前,应仔细研究一下自己的联机文档,掌握这些方法完整、正确的用法。
闽公网安备 35060202000074号