java 抛出新的 NoSuchElementException 构建失败,arraylist

java throw new NoSuchElementException build failed with arraylist

我正在使用 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.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()); 
}
}

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

只需使用stackList.isEmpty()

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

你确定它在没有异常的情况下工作吗?据我所知,您使用了输入“1”,它用一个元素填充 ArrayList。 在您的主要方法中,您先查看 (returns "1"),然后弹出 (returns "1")。然后你继续 peek 和 pop,导致下一个 peek 调用执行

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

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

您可以在 peek 函数中添加检查以查看列表中是否还有剩余元素。如果没有,将 return 设为 null 可能是个好主意。

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