【问题标题】:java throw new NoSuchElementException build failed with arraylistjava throw new NoSuchElementException build failed with arraylist
【发布时间】:2020-06-04 11:57:01
【问题描述】:

我正在使用 Arraylist 编写堆栈实现。当数组为空时尝试弹出时,我尝试使用 NoSuchElementException 但我收到构建错误消息,我不知道发生了什么。这是我得到的输出:

please enter your number: 

1 f
*********************Stack ArrayList Implementation*********************
false
1
1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:422)
    at java.util.ArrayList.get(ArrayList.java:435)
    at StackUsingArrayList.peek(StackUsingArrayList.java:42)
    at StackUsingArrayList.main(StackUsingArrayList.java:74)
C:\Users\alsrb\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 4 seconds)

这部分似乎是问题所在,有趣的是,如果我不使用异常抛出,它就可以正常工作。

int pop() {
if (!isEmpty()) { // checks for an empty Stack
      int popValue = stackList.get(stackList.size() - 1);
      stackList.remove(stackList.size() - 1); // removes the poped element             
      return popValue;
} else {
    throw new NoSuchElementException();
    //System.out.print("The stack is already empty  ");
    //return -1;
}
}

有人请帮助我。 这是我的全部代码

import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class StackUsingArrayList{
Scanner scanner = new Scanner(System.in); 


ArrayList<Integer> stackList;

StackUsingArrayList() {
    stackList = new ArrayList<>();
}


void push(int v) {
    stackList.add(v);
}

int pop() {
    if (!isEmpty()) { // checks for an empty Stack
          int popValue = stackList.get(stackList.size() - 1);
          stackList.remove(stackList.size() - 1); // removes the poped element             
          return popValue;
    } else {
        throw new NoSuchElementException();
        //System.out.print("The stack is already empty  ");
        //return -1;
    }
}

boolean isEmpty() {
    if (stackList.get(0) == null){
        return true;
    } else {
        return false;
    }
}

int peek() {
    return stackList.get(stackList.size() - 1);
}

int size(){
    int i = 0;
    while(stackList != null){
        stackList.get(i);
        i++;
    }
return i;
}


public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in); 

    StackUsingArrayList myStack = new StackUsingArrayList();

    System.out.println("Please enter your number: ");

    while(scanner.hasNextInt()){
        int x = scanner.nextInt();
        if(x >= 0){
        myStack.push(x);
        }
    }


    System.out.println("*********************Stack ArrayList Implementation*********************");
    System.out.println(myStack.isEmpty());
    System.out.println(myStack.peek());
    System.out.println(myStack.pop()); 
    System.out.println(myStack.peek()); 
    System.out.println(myStack.pop()); 
    System.out.println(myStack.peek()); 
    System.out.println(myStack.pop()); 
}
}

【问题讨论】:

  • 真的很奇怪。异常似乎在主要的peek 调用中,异常文本表示您正在尝试访问-1 数组元素。唯一的可能是stackList.size()0。我假设您输入了足够的整数来执行main 中的pop 呼叫号码。尝试调试。

标签: java exception stack throw


【解决方案1】:

您确定它可以在没有例外的情况下工作吗?据我所知,您使用了输入“1”,它用一个元素填充了 ArrayList。 在您的主要方法中,您窥视(返回“1”)然后弹出(返回“1”)。然后你继续 peek 和 pop,导致下一个 peek 调用执行

int peek() {
    return stackList.get(stackList.size() - 1);
}

由于您已经弹出列表中的唯一元素,因此列表大小为 0。这意味着您尝试返回 stackList.get(0-1),它会正确抛出 ArrayIndexOutOfBoundsException。

您可以在 peek 函数中添加一个检查,以查看列表中是否还有元素。如果不是,最好返回 null。

除此之外,看看 java 提供的 ArrayList 方法可能是个好主意。特别是 ArrayList.size() 和 ArrayList.isEmpty() 是很好的替代品。

【讨论】:

  • 感谢您的帮助。我现在正确了,但代码仍然无法构建。这是应该发生的事情吗?
  • 是构建失败,还是执行失败?
  • 我在 NetBeans 上运行代码,它显示 BUILD FAIL。
【解决方案2】:

您的isEmpty() 实现是错误的。如果stackList 为空,stackList.get(0) 会抛出异常。

只需使用stackList.isEmpty()

boolean isEmpty() {
    return stackList.isEmpty();
}

【讨论】:

    猜你喜欢
    • 2014-11-20
    • 2022-12-02
    • 2018-10-27
    • 1970-01-01
    • 2019-03-20
    • 2023-01-21
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    相关资源
    最近更新 更多