- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用链表从头开始创建了一个堆栈数据结构。似乎 push()
正确,因为我可以使用 first.data
的显示方法,并且它显示列表。但是当我使用peek()
时,我得到一个NoSuchElementException
,因为有一个if语句if(first == null)
,但first不应该是null
。
我不知道如何以不同的方式解决这个问题。
public class StackNew implements Stack{ //Stack is an interface given by my professor
private int size;
private class Node {
public Object data;
public Node next;
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
}
private Node first = null;
public void push(Object newElement) {
first = new Node(newElement, first);
size++;
}
public Object peek(){
if (first == null) {
throw new NoSuchElementException();
}
return first.data;
}
public void display() {
Node previous = null;
while(first != null) {
System.out.println((first.data));
previous = first;
first = first.next;
}
}
public static void main(String args[]) {
StackNew stack = new StackNew();
stack.push("java");
stack.push(1);
stack.push("code");
stack.display();
System.out.println(stack.isEmpty());
stack.peek();
}
当我尝试使用 peek() 方法时,第一个节点为 null,而它应该包含“code”。
最佳答案
在您的代码中,您会注意到仅当您调用 display
方法时才会收到错误。
这是因为在您的实现中,您将 first
覆盖为 first.next
,它最终在 while
循环的最后一次迭代中变为 null。因此,您需要一个新指针来跟踪当前元素,并在不影响 first
指针的情况下前进到下一个元素。
因此您需要将 display
方法更正为:
public void display() {
Node current = first;
while(current != null) {
System.out.println((current.data));
current = current.next;
}
}
这里一个新的指针current
将跟踪当前元素,并在while循环中连续打印元素。
代码中的一些其他问题(如果您尝试扩展java.util.Stack
):
实现
Stack
类,但实际上您应该扩展
它。Stack
类中的 isEmpty()
,您需要在 push()
方法中增加 elementCount++;
而不是 size
。由于 isEmpty()
检查从 Stack
类继承的 elementCount
。push
的返回类型不应为 void
,而应返回参数。 (根据Stack
类的Java doc)。
@Override
public Object push(Object newElement) {
first = new Node(newElement, first);
elementCount++;
return newElement;
}
关于java - 调用时无法识别 LinkedList 中的第一个节点数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55311525/
我想添加 LinkedList (我们称之为列表 A)到 LinkedList> (称之为列表 B)。执行此操作后,我需要更改列表 A 的值并将其再次添加到列表 B,但不更改已存储在列表 B 中的值。
更新:感谢所有的回答。我发现的最干净的解决方案是这个: if ( k(Arrays.asList(new LinkedList<>())); 我有一个递归方法,可以从列表中生成所有“n 选 k”组合。
在我的 Java 应用程序中,以下两个都将编译和运行,并产生所需的结果。 //"Rotate" the list items one place to the left. myLinkedList.a
我写了一个LinkedList接受 Nodes 的类存储 Integers . 然后我创建了一个 LinkedList stack = new LinkedList() ,并添加了 Node s 如果
这个问题在这里已经有了答案: What does it mean to "program to an interface"? (33 个答案) 关闭 9 年前。 新手 Java 问题: 谁能解释一下
我有一个问题。我无法并排输出我的 LinkedList。我问了这个问题,但遗憾的是我的老师告诉我不要更改方法头或使用 java 库(如日历)。我得到了很多关于使用它或更改方法头的建议。我是根据年级而定
这里有什么问题?。我正在尝试使用邻接列表,通过利用 util 包中的集合来实现图形数据结构。这里 LinkedList array which holds some integer. Each ele
这个问题已经有答案了: Reversing a linked list in Java, recursively (33 个回答) 已关闭10 年前。 如何使用 linkedList 类中的方法以相反
我需要实现一个 LinkedList,到目前为止,我已经编写了在列表中按顺序插入值的方法。我有我的节点 front 作为我的类的实例数据,当创建我的第一个值并尝试将 front 的 next 值设置为
目前,我的 LinkedList(不是 Java 的)类中有一个方法,可以将单个节点添加到 LinkedList 中,如下所示: public void add(int index, T v) {
我正在编写一个读取 XML 文件的类,该 XML 使用“sax”类进行解析。在我的 XML 文件中,我创建了“for”标签和“宏”,使 for 循环能够写入 XML,例如: Th
我正在处理一个 C++ 作业,我将在一个链表的链表上创建一个搜索引擎。根据要求,我不能使用其他库和 STL。 基本上它会是这样的(我从小列表中删除了变量,因为它们是不相关的): 我的结构是这些: st
老实说,我现在真的很困惑这个问题,并且真的不知道如何解决这个问题。我需要编写一个方法,其中给定一个字符链接列表(例如:{'a','A','d','X'})并返回仅包含大写字符的列表(返回:{'A','
我正在尝试获取可执行文件中的两个链表,并在交替位置将它们合并到一起。前任。 ListOne 1,2,3 和 ListTwo 4,5 新的 ListOne 应该是 1,4,2,5,3。 链表.h文件:
这个问题在这里已经有了答案: Is List a subclass of List? Why are Java generics not implicitly polymorphic? (19 个回答
在尝试了解如何将哈希表插入LinkedLists时,我遇到了麻烦。我失去了尝试过的不同事物的数量。我知道我可以使用ArrayList或其他东西,但是我想使它与LinkedLists一起工作,以便可以对
我一直在尝试编写一种方法,不仅可以从 LinkedList(allUsers) 中删除对象(User),还可以从所有用户拥有的单个 LinkedList 中删除。谁能向我解释为什么这是错误的?我已经包
我有一个列表结构和一个名为树的递归函数。在下面的代码中,它永远不会到达 current == null 语句,因此它将永远运行。 如果我无法使用null,解决方案是什么? private void t
这个问题在这里已经有了答案: How does one add a LinkedList to a LinkedList in C#? (3 个答案) 关闭 9 年前。 假设我有以下内容: Link
我正在尝试为 LinkedList 创建一个反向 ListIterator,并且打算将其实现为 linkedList.listIterator(linkedList. size()) 交换了 next
我是一名优秀的程序员,十分优秀!