gpt4 book ai didi

java - Java中LinkedList对象上使用的poll()、pollFirst()、pollLast()、removeFirst()和remove()方法之间有什么区别?

转载 作者:行者123 更新时间:2023-12-01 06:28:33 32 4
gpt4 key购买 nike

我有一个 LinkedList 对象。

我想在我的 LinkedList 对象上应用这 5 个方法。

LinkedList<String> ll = new LinkedList<String>();

1) Object o = ll.poll();
2) Object o = ll.pollFirst();
3) Object o = ll.pollLast();
4) ll.remove();
5) Object o = ll.removeFirst();

当我使用 poll()、pollFirst()、removeFirst() 时,它们会删除列表的第一项。

任何人都可以告诉我当我在 LinkedList 对象上使用这些方法时发生了什么?以及适用于 LinkedList 对象的这 5 种方法之间有什么区别。

提前致谢。

最佳答案

Javadoc 告诉您:

poll() : Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

remove() : Return the head of this list, throws NoSuchElementException if this list is empty

pollFirst() is the same as poll().

pollLast() return the last element of this list, or null if this list is empty

removeFirst() is the same as remove()

关于java - Java中LinkedList对象上使用的poll()、pollFirst()、pollLast()、removeFirst()和remove()方法之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32420943/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com