java--集合-List
一、List接口
- List是有序的集合,集合中每個元素都有對應的順序序列。List集合可使用重複元素,可以通過索引來訪問指定位置的集合元素(順序索引從0開始),List集合默認按元素的添加順序設置元素的索引,比如第一個元素的索引就是0,好似數組。
- List作爲Collection子接口當然擁有其所有方法,同時也有自己的方法:
- void add(int index,Object e):將元素e添加到List集合中的index處;
- boolean addAll(int index,Collection c):將集合c所包含的所有元素都插入在List集合的index處;
- Object get(int index):返回集合index索引處的元素;
- int indexOf(Object o):返回對象o在List集合中第一次出現位置的索引;
- int lastIndexOf(object o):返回對象o在List集合中最後一次出現的位置索引;
- Object remove(int index):刪除並返回index索引處的元素;
- Object set(int index,Object e):把集合index處的元素替換爲e對象,返回以前在指定位置的元素;
- List subList(int fromIndex,int toIndex):返回從所有fromIndex(包括)到toIndex(不包括)處所有集合元素的子集合。
二、ListIterator
- Iterator的子接口,專門用於操作List集合的輸出;
- List自己還有一個listIterator()方法,該方法返回ListIterator對象,ListIterator繼承了Iterator接口,提供了專門操作List的方法。在Iterator上額外增加的方法:
- 支持雙向輸出:
- boolean hasPrevious():返回該迭代器關聯集合是否還有上一個元素;
- Object previous():返回該迭代器的上一個元素;
三、List接口中常用類
- Vector:線程安全,但速度慢,已被ArrayList替代。
- ArrayList:線程不安全,查詢速度快。
- LinkedList:鏈表結構,增刪速度快。取出List集合中元素的方式:
- get(int index):通過腳標獲取元素。
- iterator():通過迭代方法獲取迭代器對象。
- ArrayList和Vector類都是基於數組實現的List類,Vector比較古老,被ArrayList取代了;
- ArrayList是線程不安全的,而Vector是線程安全的,但是即使這樣,也不推薦使用Vector,因爲Collections有方法可以得到線程安全的ArrayList對象;
- Collections類: static List synchronizedList(List list) 返回指定列表支持的同步(線程安全的)列表。
四、Queue接口
- 繼承Collection接口
- 模擬隊列:先進先出(FIFO);
- void add(Object e):將e插入到隊列尾部;
- Object element():獲取隊列頭部的元素;
- boolean offer(Object e):將e插入到隊列的尾部,當使用有容量限制的隊列時,此方法比add(Object e)方法更好。
- Object peek():獲取隊列頭部的元素。如果此雙端隊列爲空,則返回 null。
- Object poll():獲取並刪除隊列頭部的元素。如果此雙端隊列爲空,則返回 null。
- Object remove():獲取並刪除隊列頭部的元素。
五、LinkedList
- LinkedList實現了Deque接口,而Deque是Queue的子接口。
- Deque自定義方法:
- void addFirst(Object e):把元素插入到該雙向隊列的開頭;
- void addLast(Object e):把該元素插入到該雙向隊列的末尾。
- Object getFirst():獲取但不刪除隊列第一個元素;
- Object getLast():獲取但不刪除隊列最後一個元素;
- boolean offerFirst(Object e):將指定的元素插入到該雙向隊列的開頭;
- boolean offerLast(Object e):將指定元素插入到雙向隊列的末尾;
- Object removeFirst():刪除第一個元素
- Object removeLast():刪除最後一個元素
- Object peekFirst():獲取但不刪除隊列第一個元素,如隊列爲null,返回null;
- Object peekLast():獲取但不刪除隊列最後一個元素,如隊列爲null,返回null;
- Object pollFirst():獲取並刪除隊列第一個元素,如隊列爲null,返回null;
- Object pollLast():獲取並刪除隊列最後一個元素,如隊列爲null,返回null;
- Object pop():從此雙端隊列所表示的堆棧中彈出一個元素。
- void push(Object e):將e推入進該隊列棧中。
- Object removeFirst():獲取並刪除隊列第一個元素。
- Object removeFirstOccurrence(Object o):刪除隊列第一次出現的o元素;
- removeLast():獲取並刪除隊列最後一個元素;
- removeLastOccurrence(Object o):刪除隊列中最後一次出現的o元素;
六、根據上面的方法實現ArrayList、LinkedList、實例
ArrayList實例
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ListDemo {
public static void main(String[] args) {
/*
* boolean add(E e) 向列表的尾部添加指定的元素(可選操作)。 void add(int index, E element)
* 在列表的指定位置插入指定元素(可選操作)。 boolean addAll(Collection<? extends E> c) 添加指定
* collection 中的所有元素到此列表的結尾,順序是指定 collection 的迭代器返回這些元素的順序(可選操作)。
* boolean addAll(int index, Collection<? extends E> c) 將指定 collection
* 中的所有元素都插入到列表中的指定位置(可選操作)。
*/
List l = new ArrayList();
l.add("A");// 0
l.add("B");// 1
l.add("C");//
l.add("B");// 1
l.add(0, "D");
System.out.println(l);
/*
* Object get(int index) 返回列表中指定位置的元素。
*/
Object o = l.get(0);
System.out.println(o);
/**
* int indexOf(Object o) 返回此列表中第一次出現的指定元素的索引;如果此列表不包含該元素,則返回 -1。 int
* lastIndexOf(Object o) 返回此列表中最後出現的指定元素的索引;如果列表不包含此元素,則返回 -1。
*/
System.out.println(l.indexOf("B"));
System.out.println(l.lastIndexOf("B"));
/*
* Object remove(int index) 移除列表中指定位置的元素(可選操作)。 ,返回刪除的對象 boolean
* remove(Object o) 從此列表中移除第一次出現的指定元素(如果存在)(可選操作)。 boolean
* removeAll(Collection<?> c) 從列表中移除指定 collection 中包含的其所有元素(可選操作)。
*/
System.out.println("----刪除------");
o = l.remove(0);
System.out.println(o);
System.out.println(l);
/**
* Object set(int index, Object element) 用指定元素替換列表中指定位置的元素(可選操作)。
* 返回被替換的對象
*/
System.out.println(l.set(0, "春哥"));
System.out.println(l);
/*
* List<E> subList(int fromIndex, int toIndex) 返回列表中指定的 fromIndex(包括 )和
* toIndex(不包括)之間的部分視圖。
*/
System.out.println(l.subList(1, 3));
l.clear();
l.add("A");
l.add("A");
l.add("A");
l.add("A");
System.out.println(l);
l.remove("A");// 從此列表中移除第一次出現的指定元素
System.out.println(l);
//
l.clear();
l.add("A");
l.add("B");
l.add("C");
l.add("D");
System.out.println(l);
for(Iterator it = l.iterator(); it.hasNext();){
System.out.println("--> "+it.next());
}
/**
* ListIterator<E> listIterator() 返回此列表元素的列表迭代器(按適當順序)。
*/
ListIterator lit = l.listIterator();
System.out.println(lit.hasPrevious());//false,表示指針前沒有元素
//System.out.println(lit.previous());//獲取指針前一個元素
o = lit.next();//指針相後移動一格
System.out.println("----->"+o);
System.out.println("前一個元素索引= " + lit.previousIndex());
System.out.println("前一個元素= " + lit.previous());
/**
* 相後迭代
*/
System.out.println("----------------------");
while(lit.hasNext()){
System.out.println(lit.next());
}
/**
* 相前迭代
*/
while(lit.hasPrevious()){
System.out.println("-xian->" +lit.previous());
}
}
}
LinkedList實例
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
/**
* void addFirst(Object e):把元素插入到該雙向隊列的開頭;
void addLast(Object e):把該元素插入到該雙向隊列的末尾。
boolean offerFirst(Object e):將指定的元素插入到該雙向隊列的開頭;
boolean offerLast(Object e):將指定元素插入到雙向隊列的末尾;
Object getFirst():獲取但不刪除隊列第一個元素;
Object getLast():獲取但不刪除隊列最後一個元素;
Object peekFirst():獲取但不刪除隊列第一個元素,如隊列爲null,返回null;
Object peekLast():獲取但不刪除隊列最後一個元素,如隊列爲null,返回null;
Object removeFirst():刪除第一個元素
Object removeLast():刪除最後一個元素
Object pollFirst():獲取並刪除隊列第一個元素,如隊列爲null,返回null;
Object pollLast():獲取並刪除隊列最後一個元素,如隊列爲null,返回null
*/
LinkedList list = new LinkedList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.addFirst("0");
list.offerLast("春哥哥");
System.out.println(list);
//list.clear();
//list.removeFirst();
//list.removeLast();
list.pollFirst();
list.pollLast();
System.out.println(list);
/*
* Object peek():獲取隊列頭部的元素。如果此雙端隊列爲空,則返回 null。
Object poll():獲取並刪除隊列頭部的元素。如果此雙端隊列爲空,則返回 null。
Object remove():獲取並刪除隊列頭部的元素。
*
* */
System.out.println(list.peek());
System.out.println(list);
System.out.println(list.poll());
System.out.println(list);
}
}