当前位置: 首页 > 教程 > Java集合 >

Java List接口

精华
小牛编辑
154浏览
2023-03-14

1 什么是Java List接口

List接口是Collection的子接口。它包含用于插入和删除元素的基于索引的方法。它是ListIterator接口的工厂。

2 Java List接口的语法

public interface List<E> extends Collection<E>  

3 Java List接口的方法

方法 描述
void add(int index, E element) 用于将指定的元素插入List中的指定位置。
boolean add(E e) 用于将指定的元素追加到List的末尾。
boolean addAll(Collection<? extends E> c) 用于将指定集合中的所有元素附加到List的末尾。
boolean addAll(int index, Collection<? extends E> c) 从List的指定位置开始,它用于附加指定集合中的所有元素。
void clear() 用于从此List中删除所有元素。
boolean equals(Object o) 用于将指定的对象与List的元素进行比较。
int hashcode() 用于返回List的哈希码值。
E get(int index) 用于从List的特定位置获取元素。
boolean isEmpty() 如果List为空,则返回true,否则返回false。
int lastIndexOf(Object o) 用于返回指定元素最后一次出现在此List中的索引;如果List不包含此元素,则返回-1。
Object[] toArray() 用于以正确的顺序返回包含此List中所有元素的数组。
T[] toArray(T[] a) 用于以正确的顺序返回包含此List中所有元素的数组。
boolean contains(Object o) 如果List包含指定的元素,则返回true
boolean containsAll(Collection<?> c) 如果List包含所有指定的元素,则返回true
int indexOf(Object o) 用于返回指定元素首次出现在此List中的索引,如果List不包含此元素,则返回-1。
E remove(int index) 用于删除List中指定位置上存在的元素。    
boolean remove(Object o) 用于删除指定元素的第一次出现。    
boolean removeAll(Collection<?> c) 用于从List中删除所有元素。
void replaceAll(UnaryOperator operator) 用于将List中的所有元素替换为指定的元素。
void retainAll(Collection<?> c) 用于保留List中指定集合中存在的所有元素。
E set(int index, E element) 用于替换List中位于指定位置的指定元素。
void sort(Comparator<? super E> c) 用于根据指定的比较器对List中的元素进行排序。
Spliterator spliterator() 用于在List中的元素上创建分隔符。
List<E> subList(int fromIndex, int toIndex) 用于获取位于给定范围内的所有元素。
int size() 用于返回List中存在的元素数。

4 Java List的例子

package cn.xnip;

/**
 * 小牛知识库网: https://www.xnip.cn
 */
/**
 * Java List的例子
 */
import java.util.*;

public class Demo{

    public static void main(String args[]){
        List<String> al=new ArrayList<String>();
        al.add("eric");
        al.add("jack");
        al.add("mark");
        al.add(1,"rose");
        System.out.println("位于第二位置的元素: "+al.get(2));
        for(String s:al){
            System.out.println(s);
        }
    }
}

输出结果为:

位于第二位置的元素: jack
eric
rose
jack
mark

5 Java ListIterator接口

ListIterator接口用于在List集合中遍历元素。

5.1 Java ListIterator接口的语法

public interface ListIterator<E> extends Iterator<E>  

6 Java ListIterator接口的方法

方法 描述
void add(E e) 将指定的元素插入List。
boolean hasNext() 如果ListIterator在向前遍历列表时有更多元素,则此方法返回true。
E next() 返回List中的下一个元素并前进光标位置。
int nextIndex() 返回元素的索引,该索引将由对next() 的后续调用返回
boolean hasPrevious() 如果ListIterator在反向遍历列表时有更多元素,则此方法返回true。
E previous() 返回List中的前一个元素,并将光标位置向后移动。
E previousIndex() 返回元素的索引,该元素的索引将由后续对previous() 的调用返回。
void remove() 从next() 或previous() 方法返回的列表中删除最后一个元素
void set(E e) 用指定的元素替换next() 或previous() 方法返回的最后一个元素。

7 Java ListIterator接口例子 

package cn.xnip;

/**
 * 小牛知识库网: https://www.xnip.cn
 */
/**
 * Java listIterator的例子
 */
import java.util.*;

public class Demo{

    public static void main(String args[]){
        List<String> al=new ArrayList<String>();
        al.add("eric");
        al.add("jack");
        al.add("lucy");
        al.add(1,"mark");
        ListIterator<String> itr=al.listIterator();
        System.out.println("正向遍历元素");
        while(itr.hasNext()){

            System.out.println("index:"+itr.nextIndex()+" value:"+itr.next());
        }
        System.out.println("向后移动元素");
        while(itr.hasPrevious()){

            System.out.println("index:"+itr.previousIndex()+" value:"+itr.previous());
        }
    }
}

输出结果为:

正向遍历元素
index:0 value:eric
index:1 value:mark
index:2 value:jack
index:3 value:lucy
向后移动元素
index:3 value:lucy
index:2 value:jack
index:1 value:mark
index:0 value:eric