如果我尝试这个:
List<Integer> list = new LinkedList<>(Arrays.asList(1));
int val = list.poll();
我得到:
error: cannot find symbol
int val = list.poll();
^
symbol: method poll()
location: variable list of type List<Integer>
我不明白为什么。 val
不应该是 1
吗?我已经导入了所有必需的库
import java.util.List;
import java.util.LinkedList;
Here ,列出了函数 poll
,文档说:
检索并删除此列表的头部(第一个)。
最佳答案
问题就在这里
List<Integer> list
List类没有poll方法,它是LinkedList实现的Deque和Deque接口(interface)的一部分。在文档中,您可以通过查看“指定者:”部分来了解首先从哪个类/接口(interface)指定该方法的信息。
关于将 poll() 应用于 LinkedList 时发生 Java 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58186826/