Java:List(二)——List、ArrayList、LinkedList
阅读原文时间:2023年07月08日阅读:3

List

List接口,表明一个序列

①直接用List承接一个ArrayList或LinkedList

List list = new ArrayList <> ();

②用List.of( … )快速创建List

List list = List.of("1","2","3");

boolean

add(E e)

向List末尾加入一个元素e(相当于Python中的List.append(e))

void

add(int index , E e)

在index处插入e

boolean

addAll( Collection c )

向List末尾加入c中的所有元素

(相当于Python中的List.extend(c))

boolean

addAll(int index , Collection c)

在index处插入c中所有元素

void

clear()

移除List中的所有元素

boolean

contains(Object o)

判断List中是否包含o

boolean

contains(Collection c)

判断List中是否包含c中全部元素

boolean

equals(Object o)

判断两个List是否相等——大小、元素全部相等

E

get(int index)

获取指定索引处的元素

int

hashCode()

获取该List的hashCode

int

indexOf(Object o)

获取o在该List中的索引,如果不存在则返回-1

boolean

isEmpty()

List为空返回true

Iterator

iterator()

返回一个Iterator,可以用它遍历全部元素(具体迭代方法,可以见List(一)中所说)

int

lastIndexOf(Object o)

获取o最后一次出现时的索引,不存在则返回-1

ListIterator

listIterator()

返回一个listIterator

ListIterator

listIterator(int index)

返回一个从指定index处开始迭代的listIterator

E

remove(int index)

移除并返回指定索引处的元素

boolean

remove(Object o)

移除List中第一次出现的元素o

boolean

removeAll(Collection c)

移除List中所有在集合c中的元素

default void

replaceAll(UnaryOperator operator)

对所有List中的元素应用某个运算符,并用运算结果替换List中的元素

boolean

retainAll(Collection c)

只保留List中那些存在于集合c中的元素

E

set(int index , E element)

替换指定索引处的元素

int

size()

返回List的大小

default void

sort(Comparator c)

用指定Comparator对List中的元素进行排序

default Spliterator

splitertor()

对List中的元素创造一个Spliterator

List

subList(int start , int end)

返回[start,end)间的子List

Object[]

toArray()

返回一个array,其中包含了List中的所有元素

T[]

toArray(T[] a)

把List中的元素保存到一个指定类型T的array中

ArrayList

数组List

  1. ArrayList():建造一个空List,采用默认大小(10);

  2. ArrayList(Collection c):用一个集合构造一个ArrayList;

  3. ### ArrayList(int initialCapacity):构造一个指定大小的空List。

所有Link接口的方法

Object

clone()

返回当前ArrayList的副本

void

ensureCapacity(int minCapacity)

增大并保持当前ArrayList的容量为参数指定的值

void

forEach(Consumer action)

实现这个方法,就可以用forEach语法迭代该List

boolean

removeIf(Predicate fiter)

移除List中所有满足Predicate的元素

void

trimToSize()

压缩ArrayList的容量到当前的Size

LinkedList

链表List

  1. LinkedList():构造一个空List

  2. ### LinkedList(Collection c):用一个集合构造一个List

所有Link的方法

void

addFirst(E e)

在List开头插入元素e

void

addLast(E e)

在List末尾插入元素e

Object

clone()

返回该LinkedList的一个副本

Iterator

descendingIterator()

倒序迭代

E

element()

返回第一个元素

E

getFirst()

返回第一个元素

E

getLast()

返回最后一个元素

boolean

offer(E e)

在List末尾插入元素e

boolean

offerFirst(E e)

在List开头插入元素e

boolean

offerLast(E e)

在List末尾插入元素e

E

peek()

返回首元素

E

peekFirst()

返回首元素,List为空时返回null

E

peekLast()

返回尾元素,List为空时返回null

E

poll()

返回并删除首元素

E

pollFirst()

返回并删除首元素,List为空时返回null

E

pollLast()

返回并删除尾元素,List为空时返回null

E

pop()

用该List作为一个Stack时,执行出栈操作

void

push(E e)

入栈

E

remove()

返回并删除首元素

boolean

removeFirstOccurrence(Object o)

移除List中第一次出现的元素o

注意到一些功能重复的方法(没有列全,只举3个常用的):

  • boolean add( E )、boolean offer( E ):尾部添加;
  • E remove()、E poll():获取并删除首部;
  • E element()、E peek():获取首部但不删除;

这是因为在失败时,它们的行为不同——前者会throw IllegalStateException、后者会return false或null。

现举add与offer为例,剩下两个其实用法一致:

Queue q =…
try{
q.add("Apple");
System.out.println("添加成功");
} catch(IllegalStateException e){
System.out.println("添加失败");
}

Queue q =…
if(q.offer("Apple")){
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}