LinkedList的常见使用方法

发布时间 2023-11-29 21:27:22作者: 是小言

1、add
boolean add(E e):在链表后添加一个元素,如果成功,返回true,否则返回false;
void addFirst(E e):在链表头部插入一个元素;
addLast(E e):在链表尾部添加一个元素;
void add(int index, E element):在指定位置插入一个元素。

 

2、remove
E remove():移除链表中第一个元素;
boolean remove(Object o):移除链表中指定的元素;
E remove(int index):移除链表中指定位置的元素;
E removeFirst():移除链表中第一个元素,与remove类似;
E removeLast():移除链表中最后一个元素;
boolean removeFirstOccurrence(Object o):移除链表中第一次出现所在位置的元素;
boolean removeLastOccurrence(Object o):移除链表中最后一次出现所在位置的元素;

 

3、get
E get(int index):按照下边获取元素; 
E getFirst():获取第一个元素; 
E getLast():获取第二个元素;

 

4、push、pop、poll
void push(E e):与addFirst一样,实际上它就是addFirst;
E pop():与removeFirst一样,实际上它就是removeFirst; (当链表为空时 ,返回exception)
E poll():查询并移除第一个元素;  (当链表为空时 ,返回null) 

 

5、peek
E peek():获取第一个元素,但是不移除; 
E peekFirst():获取第一个元素,但是不移除;  (当链表为空时 ,返回null) 
E peekLast():获取最后一个元素,但是不移除; (当链表为空时 ,返回null)