- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面只是我的代码的一小块,我试图理解为什么如果我添加注释掉的代码行,它会抛出 EmptyStackException。如果一行中有两个空值(通过调用 items.getNextItem( 生成)),我需要将
)。任何洞察为什么会打破?或者我如何才能使 oneStack
顶部的项目添加到 twoStack
的顶部)oneStack
的最高值也成为此时 twoStack
的最高值?
我尝试将这行代码放入 if
中,为 oneStack.peek()
的值分配一个变量,但这些都没有帮助。几乎就像注释掉的一行正在清空整个堆栈(??)。
要点:如果在注释掉的代码行中我将 oneStack.peek()
与任何其他值交换,它就可以正常工作。那么为什么它不能与 oneStack.peek()
一起使用呢?
oneStack.push(firstItem);
twoStack.push(firstItem);
nextItem = items.getNextItem();
oneStack.push(nextItem);
twoStack.push(nextItem);
while (!done) {
if (oneStack.peek() == null) {
oneStack.pop();
oneStack.pop();
twoStack.pop();
twoStack.push(oneStack.peek()); // the commented out line below causes this line to throw an EmptyStackException if uncommented.
newItem = items.getNextItem();
oneStack.push(nextItem);
if (oneStack.peek() == null) {
oneStack.pop();
twoStack.pop();
//twoStack.push(oneStack.peek()); // if I uncomment this it breaks, but this needs to happen for twoStack to be correct
} else {
twoStack.push(nextItem);
}
} else if (oneStack.peek() == targetItem) {
done = true;
} else {
nextItem = items.getNextItem();
oneStack.push(nextItem);
twoStack.push(nextItem);
}
这是生成项目的方式:
项目1、项目2、项目3、项目4、项目5、空、空、项目6、项目7
最后,这是堆栈剩下的内容:oneStack:项目1、项目2、项目6、项目7
twoStack:项目1、项目2、项目3、项目4、项目5、项目4、项目6、项目7(item3 缺失)
twoStack 应该是:item1、item2、item3、item4、item5、item4、item3、item6、item7
最佳答案
我建议在尝试从堆栈中弹出任何内容或查看之前,首先检查它是否不为空。至少它可以防止异常。
关于Java Stack 在推送后抛出 EmptyStackException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42080236/
我有以下部分代码来初始化堆栈数组: vDec.setRoute(new Stack[vDec.getIndexes().length]); for(i=0;i(); } 当我检查 while 循环内是
我的程序采用后缀表达式并将其更改为中缀表达式。 我在代码中包含了两个错误原因,即程序没有足够的运算符以及输入不是有效的数字或运算符。 当我输入不好的输入时,错误会被捕获,但是,当在扫描仪中输入正确的输
当我尝试运行后缀表达式时,由于我的字符串有两个背靠背的操作数,因此出现 EmptyStackException。我不知道如何在我的堆栈中捕获它。任何想法或想法都值得赞赏。 import java.ut
我正在用 Java 构建一个 treap 类。下面是我向陷阱添加新节点的函数。过程是:向下遍历到treap的底部(同时将路径中的每个节点添加到本地堆栈)首先只关心BST结构,然后,一旦到达底部,我将通
下面只是我的代码的一小块,我试图理解为什么如果我添加注释掉的代码行,它会抛出 EmptyStackException。如果一行中有两个空值(通过调用 items.getNextItem( 生成)),我
https://www.hackerrank.com/challenges/counting-valleys/problem存在计数谷问题我知道我的不是最好的解决方案,但它在我的系统上使用示例测试用例
我很困惑为什么当我将元素插入 ArrayList 时会抛出异常...这一定是我的 Push() 的问题方法,有人能找到问题吗?我尝试在 if 语句周围加上大括号,但没有成功,甚至可能是 empty()
我在应用程序中使用抛出 java.util.EmptyStackException 的第 3 方库 这只发生在虚拟机 JIT/预热期间 开始申请 开始压力测试,不增加压力。 抛出 java.util.
我是新来的,我有一个问题。我正在尝试实现一个比较器来按顶部比较两个堆栈。代码如下所示 class Comp implements Comparator> { @Override pub
我正在用 Java 实现 Stack。对于 pop 操作,我想抛出 EmptyStackException。根据documentation 、 EmptyStackException 有一个构造函数,
我想做的是让 eval 变量将它的每个字母放入堆栈中,然后打印出来。我收到 EmptyStackException 错误(假设这意味着堆栈中没有任何内容)。我不明白的是,我以为eval字符串被放入了变
我有一个由多个线程处理的 Stack 对象。其中一个线程是工作线程,它对 Stack 对象执行弹出操作。我想处理 Stack 为空的情况,我看到两个选项 try{ Object obj = (
下面是一段java代码,它接受中缀格式的表达式字符串并将其转换为后缀格式。 package post; import java.util.Stack; public class InfixtoPost
我有一个在 Oracle weblogic 中运行的 Java 项目。我们正在使用 apache-commons-chains。当尝试解析 XML 时,我们会间歇性地收到 EmptyStackExce
我正在 3 x 3 网格布局面板中创建一个数字益智游戏。首先我随机 8 个数字并将它们压入堆栈,然后弹出它们的值并将它们设置为按钮文本。应用程序运行良好,但有时我会打印出以下错误: Exception
我是一名优秀的程序员,十分优秀!