List使用
测试例子:TestList
package ds.collections.lists;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
/**
* 测试List
* 基础,使用,用法
* @author Hust
* @Time 2011-11-7
*/
public class TestList {
//--ArrayList / LinkedList / Vector
public static void main(String[] args) {
//linkedlistTest();
//arrayListTest();
vectorTest();
}
/**
* vector
* 线程安全,
*/
public static void vectorTest(){
Vector<String> v = new Vector<String>();
v.addElement("v1");
v.addElement("v2");
v.add("v5");
System.out.println("当前容量"+v.capacity()); //10
System.out.println(v.contains("v2")); //true
System.out.println(v.size());//3
System.out.println(v.elementAt(0));
System.out.println(v.get(0));
System.out.println(v.firstElement());
System.out.println(v.lastElement());
System.out.println(v.indexOf("v5"));
System.out.println(v.set(1, "vSet"));
//遍历
for (Enumeration<String> e = v.elements(); e.hasMoreElements();)
System.out.println(e.nextElement());
//Enumeration<String> e = v.elements();
//遍历2
Iterator<String> it = v.iterator();
while (it.hasNext()) {
System.out.println( it.next());
}
}
/**
* ArrayList
* 基于数组,插入快,按照index读取快 删除或者无序插入慢
*/
public static void arrayListTest(){
//不是同步的: List list = Collections.synchronizedList(new ArrayList());
//基于数组,可在初始化时指定其大小,0.75
List<String> list = new ArrayList<String>(8);
list.add("a");
list.add("b");
list.add("c");
list.add("d");
//遍历
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next() + " ");
}
System.out.println(list.get(1)+list.size()); // b 4
ArrayList<String> arrList = new ArrayList<String>(list);
@SuppressWarnings("unchecked")
ArrayList<String> arrList2 = (ArrayList<String>) arrList.clone();
arrList.set(2, "set2");
System.out.println(arrList.indexOf("set2")); //2
System.out.println(arrList2.indexOf("set2")); // -1
System.out.println(arrList.contains("set2")); //true
System.out.println(arrList2.contains("set2")); //false
//list.clear();
for(Iterator<String> iterator = arrList.iterator(); iterator.hasNext();) {
System.out.println(iterator.next() + " ");
}
list.clear(); //清空list
System.out.println(list.isEmpty()); //true
System.out.println(arrList.isEmpty()); //false
}
/**
* linkedList
* 基于链表,随机访问较慢,插入,删除快.可以当作堆栈、队列和双向队列使用
*/
public static void linkedlistTest() {
//部分代码借鉴于:http://jonsion.iteye.com/blog/422033
/***
* 实现不是同 步的,可:List list = Collections.synchronizedList(new LinkedList());
* add() addFirst()均在列表头插入元素 addAll
* add(index,value)在指定位置插入元素 addAll(index,values)
* addLast()在列表尾插入元素
*
*/
List<String> linkList = new LinkedList<String>();
for (int i = 0; i < 10; i++) { //添加
linkList.add(String.valueOf(i + 10));
}
linkList.add("1111"); //在List后再加一元素
System.out.println("遍历一:for (int i = 0; i < linkList.size(); i++) linkList.get(i)");
for (int i = 0; i < linkList.size(); i++) {
//System.out.println("linkList index " + linkList.get(i));
}
linkList.remove(0);//删除指定位置的元素,同数组,第一个元素下标为0
Iterator<String> iterator = linkList.iterator();
System.out.println("遍历二:Iterator<String> iterator = linkList.iterator(); iterator.next() ");
while (iterator.hasNext()) {
iterator.next();
//System.out.println("linkList Iterator " + iterator.next());
}
linkList.add(2, "1111"); //指定位置添加元素
System.out.println("遍历三:for (String tmpstr : linkList) tmpstr ");
for (String tmpstr : linkList) {
//System.out.println("linkList : " + tmpstr);
}
//直接拷贝得了
LinkedList<String> linkedlist = new LinkedList<String>(linkList);
linkedlist.addFirst("addFirst");//在LinkedList
System.out.println("list大小"+linkedlist.size());
System.out.println("是否包含元素"+linkedlist.contains("addFirst"));
System.out.println("取第一个元素值"+linkedlist.getFirst());
System.out.println("删除第一个元素其值为"+linkedlist.removeFirst());
System.out.println("取最后一个元素值"+linkedlist.getLast());
System.out.println("删除最后一个元素其值为"+linkedlist.removeLast());
System.out.println("设置指定位置元素的值"+linkedlist.set(2,"set2"));
System.out.println("元素出现的位置"+linkedlist.indexOf("set2"));//lastIndexOf
/**
* list大小12
是否包含元素true
取第一个元素值addFirst
删除第一个元素其值为addFirst
取最后一个元素值1111
删除最后一个元素其值为1111
设置指定位置元素的值1111
元素出现的位置2
*/
iterator = linkedlist.iterator();
while (iterator.hasNext()) {
System.out.println("linkedlist Iterator " + iterator.next());
}
}
}