List接口分析

类别:Java 点击:0 评论:0 推荐:

List接口是继承自Collection接口的,有关Collection接口:
http://blog.csdn.net/treeroot/archive/2004/09/09/99591.aspx

List是一种有序的Collection,可以通过索引访问集合中的数据,看看List中
有哪些方法

1.int size()
从Collection中继承

2 boolean isEmpty()
从Collection中继承

3.boolean contains(Object o)
从Collection中继承

4.Iterator iterator()
从Collection中继承

5.Object[] toArray()
从Collection中继承

6.Object[] toArray(Object a[])
从Collection中继承

7.boolean add(Object o)
从Collection中继承
添加到List末尾

8.boolean remove(Object o)
从Collection中继承

9.boolean containsAll(Collection c)
从Collection中继承

10.boolean addAll(Collection c)
从Collection中继承
添加到List末尾,相当于方法addAll(size(),Collection c)

11.boolean addAll(int index, Collection c)
在指定索引处添加集合中的所有元素,原列表中索引后(包括当前索引)的元素后移

12. boolean removeAll(Collection c)
从Collection中继承

13. boolean retainAll(Collection c)
从Collection中继承

14.void clear()
从Collection中继承

15.boolean equals(Object o)
从Collection中继承

16.int hashCode()
从Collection中继承

17.Object get(int index)
通过索引号返回指定元素

18.Object set(int index, Object element)
把指定索引处的元素替换为新的元素,返回原来的被替换的元素,注意索引不能越界,
不要试图用set(size(),newElement)来插入数据。

19.void add(int index, Object element)
在指定索引处插入一个元素,原来该索引处元素以及后面的元素后移。

20.Object remove(int index)
删除指定索引处的元素,后面的元素前移。

21.int indexOf(Object o)
返回指定元素在列表中的索引(最小值),如果不存在该元素,返回-1。

22.int lastIndexOf(Object o)
和上面差不多,返回的是最大值。

23.ListIterator listIterator()
返回列表迭代器,相当于ListIterator(0)。

24.ListIterator listIterator(int index)
返回指定初始位置的列表迭代器。

25.List subList(int fromIndex, int toIndex)
返回当前List的一个视图,这里不说是返回一个子列表,而说是一个视图,因为
对返回的List的非接口修改会影响到原来的List。

List比Collection多了10个方法,主要是有关索引的方法。
1).所有的索引返回的方法都有可能抛出一个IndexOutOfBoundsException异常
2).subList(int fromIndex, int toIndex)返回的是包括fromIndex,不包括toIndex的视图,该列表的size()=toIndex-fromIndex。

本文地址:http://com.8s8s.com/it/it15496.htm