指令 无效 调试 @stack 队列

huangapple go评论94阅读模式
英文:

commands invalid debugged @stack queue

问题

我在处理我的堆栈程序时遇到了问题,我真的不理解我的错误。我能得到一些帮助吗?每次我尝试运行它时,都会出现多个错误,我对其中的一些事情感到非常困惑。我有总共 60 个错误。因此整个程序都有问题。我所尝试做的就是在这个堆栈和队列程序中创建一些名字。

  1. public class YourStackNB {
  2. private int maxSize;
  3. private long[] stackArray;
  4. private int top;
  5. public YourStackNB(int s) {
  6. maxSize = s;
  7. stackArray = new long[maxSize];
  8. top = -1;
  9. }
  10. public void push(long j) {
  11. stackArray[++top] = j;
  12. }
  13. public long pop() {
  14. return stackArray[top--];
  15. }
  16. public boolean isEmpty() {
  17. return (top == -1);
  18. }
  19. public static void main(String[] args) {
  20. YourStackNB theStack = new YourStackNB();
  21. Stack<Name> even = new Stack<>();
  22. theStack.push(dino);
  23. theStack.push(perry);
  24. theStack.push(jada);
  25. theStack.push(holly);
  26. theStack.push(nori);
  27. while (!theStack.isEmpty()) {
  28. long value = theStack.pop();
  29. System.out.print(value);
  30. System.out.print(" ");
  31. }
  32. System.out.println("");
  33. }
  34. }
英文:

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.

  1. public class YourStackNB {
  2. private int maxSize;
  3. private long[] stackArray;
  4. private int top;
  5. public YourStackNB(int s) {
  6. maxSize = s;
  7. stackArray = new long[maxSize];
  8. top = -1;
  9. }
  10. public void push(long j) {
  11. stackArray[++top] = j;}
  12. public long pop() {
  13. return stackArray[top--];
  14. }
  15. public boolean isEmpty() {
  16. return (top == -1);
  17. }
  18. public static void main(String[] args) {
  19. YourStackNB theStack = new YourStackNB();
  20. Stack&lt;Name&gt; even = new Stack&lt;&gt;();
  21. theStack.push(dino);
  22. theStack.push(perry);
  23. theStack.push(jada);
  24. theStack.push(holly);
  25. theStack.push(nori);
  26. while (!theStack.isEmpty()) {
  27. long value = theStack.pop();
  28. System.out.print(value);
  29. System.out.print(&quot; &quot;);
  30. }
  31. System.out.println(&quot;&quot;);
  32. }
  33. }

答案1

得分: 0

你正在尝试使用数组实现栈。但是你没有正确使用它。相反,你试图使用java.util.Stack,并传递了错误的类型Name。

另一件事是,你试图实现long数据类型的栈。但是你却在尝试放一些字符串进去。这些代码是不正确的。

  1. // Stack&lt;Name&gt; even = new Stack&lt;&gt;();
  2. // theStack.push(dino);
  3. // theStack.push(perry);
  4. // theStack.push(jada);
  5. // theStack.push(holly);
  6. // theStack.push(nori);

我建议你尝试编写一些基本的Java程序,以便更熟悉Java语法,这将有助于你更好地思考。

仅供你学习,我已经修复了错误,这段代码可以以更好的方式实现:

  1. public class YourStackNB {
  2. private int maxSize;
  3. private long[] stackArray;
  4. private int top;
  5. public YourStackNB(int s) {
  6. maxSize = s;
  7. stackArray = new long[maxSize];
  8. top = -1;
  9. }
  10. public void push(long j) {
  11. stackArray[++top] = j;
  12. }
  13. public long pop() {
  14. return stackArray[top--];
  15. }
  16. public boolean isEmpty() {
  17. return (top == -1);
  18. }
  19. public static void main(String[] args) {
  20. YourStackNB theStack = new YourStackNB(10);
  21. theStack.push(100);
  22. theStack.push(200);
  23. theStack.push(300);
  24. theStack.push(400);
  25. theStack.push(500);
  26. while (!theStack.isEmpty()) {
  27. long value = theStack.pop();
  28. System.out.print(value);
  29. System.out.print(" ");
  30. }
  31. System.out.println();
  32. }
  33. }

输出:

  1. 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

  1. // Stack&lt;Name&gt; even = new Stack&lt;&gt;();
  2. // theStack.push(dino);
  3. // theStack.push(perry);
  4. // theStack.push(jada);
  5. // theStack.push(holly);
  6. // 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

  1. public class YourStackNB {
  2. private int maxSize;
  3. private long[] stackArray;
  4. private int top;
  5. public YourStackNB(int s) {
  6. maxSize = s;
  7. stackArray = new long[maxSize];
  8. top = -1;
  9. }
  10. public void push(long j) {
  11. stackArray[++top] = j;}
  12. public long pop() {
  13. return stackArray[top--];
  14. }
  15. public boolean isEmpty() {
  16. return (top == -1);
  17. }
  18. public static void main(String[] args) {
  19. YourStackNB theStack = new YourStackNB(10);
  20. theStack.push(100);
  21. theStack.push(200);
  22. theStack.push(300);
  23. theStack.push(400);
  24. theStack.push(500);
  25. // Stack&lt;Name&gt; even = new Stack&lt;&gt;();
  26. // theStack.push(dino);
  27. // theStack.push(perry);
  28. // theStack.push(jada);
  29. // theStack.push(holly);
  30. // theStack.push(nori);
  31. while (!theStack.isEmpty()) {
  32. long value = theStack.pop();
  33. System.out.print(value);
  34. System.out.print(&quot; &quot;);
  35. }
  36. System.out.println(&quot;&quot;);
  37. }
  38. }

> 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.

huangapple
  • 本文由 发表于 2020年9月18日 12:00:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63949131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定