List 接口(重点)
在整个集合中List 是Collection 的子接口,里面的所有内容都是允许重复的。
List 子接口的定义:
public interface List extends Collection
此接口上依然使用了泛型技术。此接口对于Collection 接口来讲有如下的扩充方法
方法名称 描述
public void add(int index,E element) 在指定位置处增加元素
boolean addAll(int index,Collection<? extends E>c)
在指定位置处增加一组元素
public E get(int index) 根据索引位置取出每一个元素
public int indexOf(Object o) 根据对象查找指定位置,查不到返回 -1
public int lastIndexOf(Object o) 从后面向前查找位置,找不到返回 -1
public ListIterator<E> listIterator() 返回ListIterator 接口的实例
public ListIterator<E> listIterator(int index) 返回从指定位置的ListIterator 接口的实例
public E remove(int index) 删除指定位置的内容
public E set(int index,E element) 修改指定位置的内容
List<E> subList(int fromIndex,int toIndex) 返回子集合
在List 接口中有以上 10 个方法是对已有的Collection接口 进行扩充。
所以,List接口拥有比Collection 接口更多的操作方法。
了解了List 接口之后,那么该如何使用该接口?需要找到此接口的实现类,常用的实现类有如下几个:
ArrayList 、Vector、LinkedList