list接口及实现类
java.util
接口 List<E>
-
所有超级接口:
- Collection<E>, Iterable<E>
-
所有已知实现类:
- AbstractList, AbstractSequentialList, ArrayList,
-
AttributeList,
CopyOnWriteArrayList,
LinkedList,
- RoleList, RoleUnresolvedList, Stack, Vector
方法摘要 | ||
---|---|---|
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 中的所有元素都插入到列表中的指定位置(可选操作)。 | |
void | clear() 从列表中移除所有元素(可选操作)。 | |
boolean | contains(Object o) 如果列表包含指定的元素,则返回 true。 | |
boolean | containsAll(Collection<?> c) 如果列表包含指定 collection 的所有元素,则返回 true。 | |
boolean | equals(Object o) 比较指定的对象与列表是否相等。 | |
E | get(int index) 返回列表中指定位置的元素。 | |
int | hashCode() 返回列表的哈希码值。 | |
int | indexOf(Object o) 返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。 | |
boolean | isEmpty() 如果列表不包含元素,则返回 true。 | |
Iterator<E> | iterator() 返回按适当顺序在列表的元素上进行迭代的迭代器。 | |
int | lastIndexOf(Object o) 返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。 | |
ListIterator<E> | listIterator() 返回此列表元素的列表迭代器(按适当顺序)。 | |
ListIterator<E> | listIterator(int index) 返回列表中元素的列表迭代器(按适当顺序),从列表的指定位置开始。 | |
E | remove(int index) 移除列表中指定位置的元素(可选操作)。 | |
boolean | remove(Object o) 从此列表中移除第一次出现的指定元素(如果存在)(可选操作)。 | |
boolean | removeAll(Collection<?> c) 从列表中移除指定 collection 中包含的其所有元素(可选操作)。 | |
boolean | retainAll(Collection<?> c) 仅在列表中保留指定 collection 中所包含的元素(可选操作)。 | |
E | set(int index, E element) 用指定元素替换列表中指定位置的元素(可选操作)。 | |
int | size() 返回列表中的元素数。 | |
List<E> | subList(int fromIndex, int toIndex) 返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。 | |
Object[] | toArray() 返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素)。 | |
| toArray(T[] a) 返回按适当顺序(从第一个元素到最后一个元素)包含列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。 |
package com.Collectiom;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
/**
* list元素有序 元素可以重复 因为有索引!
* @author 小明
* list特有方法:
* add(index,element)
* addAll(index,Collection)
* remove(index)
* set(index,element)
* get(index)
* subList(from,index)
* listIterator()
*/
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
//list.add(0, "f");
//list.remove(0);
System.out.println(list.get(0));
// List<String> subList = list.subList(0, 2);
// System.out.println(subList);
ListIterator<String> listIterator = list.listIterator();
while(listIterator.hasNext()){
System.out.println("iterator------>"+listIterator.next());
}
List<String> list2 = new ArrayList<String>();
list2.add("1");
//list.addAll(list2);
System.out.println(list);
}
}