java-API之集合4——List接口以及其子接口ArrayList和LinkedList接口

List接口、ArrayList接口和LinkedList接口

List接口是ArrayList接口和LinkedList接口的父接口,他们的继承关系和List接口的特点,具体关系参考:java-API之集合2 

 List接口

特有方法:

  • 添加元素
    • void add(int index, E element) 在列表的指定位置插入指定元素(可选操作)。
    • boolean addAll(int index, Collection<? extends E> c)  将指定 collection 中的所有元素都插入到列表中的指定位置(可选操作)。
  • 获取元素
    • E get(int index)  返回列表中指定位置的元素。
    • List<E> subList(int fromIndex, int toIndex)  返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。
    • List<E>  subList(int fromIndex, int toIndex)  返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。
  • 替换元素
    • E set(int index, E element)  用指定元素替换列表中指定位置的元素(可选操作)。
  • 其它
    • int indexOf(Object o)  返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。
    • int lastindexOf(Object o)   返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。
    • ListIterator<E> listIterator()   返回此列表元素的列表迭代器(按适当顺序)。
    • ListIterator<E> listIterator(int index)  返回列表中元素的列表迭代器(按适当顺序),从列表的指定位置开始。
public class Test_List {

    public static void main(String[] args) {
        // 1、创建List对象
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(4);
        list.add(3);
        list.add(5);
        list.add(9);
        //1、接口中元素都是有序的
        System.out.println(list); // [1, 2, 4, 3, 5, 9]
        // 2、List接口中的元素都要下标,从0开始
        list.add(4,100);
        System.out.println(list); // [1, 2, 4, 3, 100, 5, 9]
        
        System.out.println(list.get(2));  // 4 获取指定下标对应的元素
        
        list.add(5);
        list.add(3);
        list.add(4);
        // 3、List接口允许存放重复元素
        System.out.println(list); // [1, 2, 4, 3, 100, 5, 9, 5, 3, 4]
        System.out.println(list.indexOf(5));  //5 第一次出现的索引
        System.out.println(list.lastIndexOf(5));  //7 最后一次出现的索引
        
        System.out.println(list.remove(4));  // 100     把指定索引的元素删除,并返回删除的元素
        
        System.out.println(list.set(3, 200)); //3    替换指定位置的元素,返回被替换的元素
        System.out.println(list); //  [1, 2, 4, 200, 5, 9, 5, 3, 4]
        
        List<Integer> sublist = list.subList(2, 7);  //截取子集合   含头不含尾 [2,7)
        System.out.println(sublist);//   [4, 200, 5, 9, 5] 
        
        list.addAll(2, sublist);  //在指定位置插入参数集合的元素
        System.out.println(list); // [1, 2, 4, 200, 5, 9, 5, 4, 200, 5, 9, 5, 3, 4]
        
        
        // list接口特有的迭代方式
        ListIterator listIt = list.listIterator();
        while(listIt.hasNext()) {
            // 遍历输入List集合中的所有元素
            System.out.println(listIt.next());  
        }        

    }

 

 ListIterator迭代器

常用方法:

  • boolean  hasNext()  以正向遍历列表时,如果列表迭代器有多个元素,则返回 true(换句话说,如果 next 返回一个元素而不是抛出异常,则返回 true
  • E next()  返回列表中的下一个元素。
  • int nextIndex()   返回对 next 的后续调用所返回元素的索引。
  • boolean  hasPrevious()  如果以逆向遍历列表,列表迭代器有多个元素,则返回 true
  • E previous()  返回列表中的前一个元素。
  • int previousIndex()  返回对 previous 的后续调用所返回元素的索引。

从上述中,可以看到ListIterator中增加了向前遍历的功能,但是最常用的还是向后遍历。

具体迭代测试代码演示参考:java-API之集合3——Collection接口

posted @ 2020-03-09 07:50  技术狂-CYL  阅读(211)  评论(0)    收藏  举报