英文:
commands invalid debugged @stack queue
问题
我在处理我的堆栈程序时遇到了问题,我真的不理解我的错误。我能得到一些帮助吗?每次我尝试运行它时,都会出现多个错误,我对其中的一些事情感到非常困惑。我有总共 60 个错误。因此整个程序都有问题。我所尝试做的就是在这个堆栈和队列程序中创建一些名字。
public class YourStackNB {
private int maxSize;
private long[] stackArray;
private int top;
public YourStackNB(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
}
public long pop() {
return stackArray[top--];
}
public boolean isEmpty() {
return (top == -1);
}
public static void main(String[] args) {
YourStackNB theStack = new YourStackNB();
Stack<Name> even = new Stack<>();
theStack.push(dino);
theStack.push(perry);
theStack.push(jada);
theStack.push(holly);
theStack.push(nori);
while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}
英文:
Im having trouble with my stack program and I don't really understand my errors. Can I please get some help? Every time i try to run it, it has multiple errors and im extremely confused on a few things. the errors i have is a total of 60 errors.
therefore the whole program is off. All im trying to do is to create names in this stack and queue program.
public class YourStackNB {
private int maxSize;
private long[] stackArray;
private int top;
public YourStackNB(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;}
public long pop() {
return stackArray[top--];
}
public boolean isEmpty() {
return (top == -1);
}
public static void main(String[] args) {
YourStackNB theStack = new YourStackNB();
Stack<Name> even = new Stack<>();
theStack.push(dino);
theStack.push(perry);
theStack.push(jada);
theStack.push(holly);
theStack.push(nori);
while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}
答案1
得分: 0
你正在尝试使用数组实现栈。但是你没有正确使用它。相反,你试图使用java.util.Stack,并传递了错误的类型Name。
另一件事是,你试图实现long数据类型的栈。但是你却在尝试放一些字符串进去。这些代码是不正确的。
// Stack<Name> even = new Stack<>();
// theStack.push(dino);
// theStack.push(perry);
// theStack.push(jada);
// theStack.push(holly);
// theStack.push(nori);
我建议你尝试编写一些基本的Java程序,以便更熟悉Java语法,这将有助于你更好地思考。
仅供你学习,我已经修复了错误,这段代码可以以更好的方式实现:
public class YourStackNB {
private int maxSize;
private long[] stackArray;
private int top;
public YourStackNB(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
}
public long pop() {
return stackArray[top--];
}
public boolean isEmpty() {
return (top == -1);
}
public static void main(String[] args) {
YourStackNB theStack = new YourStackNB(10);
theStack.push(100);
theStack.push(200);
theStack.push(300);
theStack.push(400);
theStack.push(500);
while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println();
}
}
输出:
500 400 300 200 100
由于栈是后进先出(LIFO),因此它以我们插入元素的相反顺序打印输出。
英文:
You are trying to implement stack using array. But you are not using it properly. Rather you are trying to use java.util.Stack with passing incorrect type Name.
Another thing you are trying to implement Stack of long data type. But you are trying to put some string there. These codes are incorrect
// Stack<Name> even = new Stack<>();
// theStack.push(dino);
// theStack.push(perry);
// theStack.push(jada);
// theStack.push(holly);
// theStack.push(nori);
I can suggest you please try to write some basic java program so that you are more aware of java syntax and it will help you to better thinking.
Just for your learning I am fixing the errors and this code can be implemented in better way
public class YourStackNB {
private int maxSize;
private long[] stackArray;
private int top;
public YourStackNB(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;}
public long pop() {
return stackArray[top--];
}
public boolean isEmpty() {
return (top == -1);
}
public static void main(String[] args) {
YourStackNB theStack = new YourStackNB(10);
theStack.push(100);
theStack.push(200);
theStack.push(300);
theStack.push(400);
theStack.push(500);
// Stack<Name> even = new Stack<>();
// theStack.push(dino);
// theStack.push(perry);
// theStack.push(jada);
// theStack.push(holly);
// theStack.push(nori);
while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}
> OUTPUT:
> 500 400 300 200 100
As stack is last in first out(LIFO) thus it's printing in reverse order we inserted the element into it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论