1.List集合
1.1 List集合中元素有次序、可以重复,可以通过索引来访问集合中的元素。默认按照元素的添加顺序来设置元素的序号,序号从0开始。
1.2 List做为Collection的子接口,继承了Collection接口的所有方法。同时由于List集合的有序性,可以通过序号来访问、删除、设置、获取指定元素序号、获取指定序号元素子集等。具体代码,如下:
List<String> strLists = new ArrayList<String>();
String[] str = {"g","h","k"};
List<String> arrayToList = Arrays.asList(str);
//指定位置插入元素
strLists.add(0, "c"); //新建集合若是不从第0个开始插入会报越界错误
strLists.add(1, "a");
strLists.add(2, "b");
System.out.println(strLists);
//指定位置插入集合
strLists.addAll(1, arrayToList);
System.out.println(strLists); //输出:[c, g, h, k, a, b]
System.out.println(strLists.get(2)); //输出:h
//返回指定元素的序号
System.out.println("h:"+strLists.indexOf("h")); //输出 h:2
//元素最后一次出现的位置
strLists.add("g");
System.out.println("g最后一次出现的序号:"+strLists.lastIndexOf("g")); //输出:g最后一次出现的序号:6
System.out.println("g第一次出现的序号:"+strLists.indexOf("g")); //输出:g第一次出现的序号:1
//移除指定位置的元素
strLists.remove(0);
System.out.println(strLists); //输出:[g, h, k, a, b, g]
//移除指定元素
strLists.remove("k");
System.out.println(strLists); //输出:[g, h, a, b, g]
//设置某个位置的元素
strLists.set(0, "j");
System.out.println(strLists); //输出:[j, h, a, b, g]
//获取集合的子集
System.out.println(strLists.subList(0, strLists.size()-2)); //输出:[j, h, a]
1.3 List集合除了有Collection具有的Interator外,还有ListInterator迭代器,多了方法previous()和hasPrevious(),可以实现向前迭代。
//listIterator
List<String> listIn = new ArrayList<String>();
listIn.add("a");
listIn.add("b");
listIn.add("c");
ListIterator<String> listRator = listIn.listIterator();
//顺序输出
while(listRator.hasNext())
{
System.out.print(" "+listRator.next());
}
System.out.println();
System.out.println("========逆序了=======");
//逆序输出
while(listRator.hasPrevious())
{
System.out.print(" "+listRator.previous());
}
1.4 ArrayList常用方法
修饰符与类型 | 方法与描述 |
---|---|
boolean | add(E e)
Appends the specified element to the end of this list.
|
void | add(int index,E element)
Inserts the specified element at the specified position in this list.
|
boolean | addAll(Collection<? extendsE> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
|
boolean | addAll(int index,Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.
|
void | clear()
Removes all of the elements from this list.
|
Object | clone()
Returns a shallow copy of this
ArrayList instance.
|
boolean | contains(Object o)
Returns
true if this list contains the specified element.
|
void | ensureCapacity(int minCapacity)
Increases the capacity of this
ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
|
E | get(int index)
Returns the element at the specified position in this list.
|
int | indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
|
boolean | isEmpty()
Returns
true if this list contains no elements.
|
Iterator<E> | iterator()
Returns an iterator over the elements in this list in proper sequence.
|
int | lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
|
ListIterator<E> | listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
|
ListIterator<E> | listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
|
E | remove(int index)
Removes the element at the specified position in this list.
|
boolean | remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
|
boolean | removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection.
|
protected void | removeRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between
fromIndex , inclusive, and
toIndex , exclusive.
|
boolean | retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection.
|
E | set(int index,E element)
Replaces the element at the specified position in this list with the specified element.
|
int | size()
Returns the number of elements in this list.
|
List<E> | subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified
fromIndex , inclusive, and
toIndex , exclusive.
|
Object[] | toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
|
<T> T[] | toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
|
void | trimToSize()
Trims the capacity of this
ArrayList instance to be the list's current size.
|
1.6 Arrays.toList()方法会将数组转换成固定大小的list集合,若是在转换的list中删除、增加元素时会报错。
1.7 对应list的遍历效率,ArrayList使用get方法遍历效率较高,适合随机方法,LinkedList使用迭代遍历效率较高。