Java-获取 IndexOutOfBoundsException

标签 java indexoutofboundsexception

我在以下代码的第 22 行中遇到 IndexOutOfBoundsException 的问题。该程序的目的是充当具有以下优先顺序的表达式求值器:

 Operator| Description | Level | Associativity 
 --------+-------------+-------+---------------
    &    | Bitwise AND |   1   | left to right
    ^    | Bitwise XOR |   2   | left to right
    |    | Bitwise OR  |   3   | left to right

如果使用扫描器类给程序提供像“1 | 8 & 3”这样的输入表达式,那么程序应该解析这个输入并将结果输出为 1。

示例输出:

Enter the expression: 1 | 8 & 3
Result of the given expression: 1

说明:

评估的方式是:

8 & 3 = 1000 & 0011 = 0000 = 0

1 | 0 = 0001 | 0000 = 1

我一直在努力让程序正常运行。下面是我最新的代码:

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

public class Problem1 {
   public static void main(String[] args) throws Exception {

    try {
        ArrayList<String> inputarray = new ArrayList<String>();
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);

        System.out.print("Enter an expression with single digit integers (example: \"1 | 8 & 3\"): ");

        if(input.hasNextLine()) {
            inputarray.add(input.nextLine());
        }

        int j = 1;

        j = 1;
        for(int i = 0; i < inputarray.size(); i++) {
            if(inputarray.get(j) == "&") {
                inputarray.add((j+1), Integer.toString(Integer.parseInt(inputarray.get(j-1)) & Integer.parseInt(inputarray.get(j+1))));
                System.out.println(" Result of the given expression: " + inputarray);
                inputarray.remove((j-1));
                inputarray.remove((j));
            }
            if(j <= (inputarray.size() - 3)) {
                j += 2;
            }
            else if(j == (inputarray.size() - 2)) {
                break;
            }
        }

        j = 1;
        for(int i = 0; i < inputarray.size(); i++) {
            if(inputarray.get(j) == "^") {
                inputarray.remove(j);
                inputarray.add(j, Integer.toString(Integer.parseInt(inputarray.get(j-1)) ^ Integer.parseInt(inputarray.get(j+1))));
                inputarray.remove((j-1));
                inputarray.remove((j+1));
            }
            if(j <= (inputarray.size() - 3)) {
                j += 2;
            }
            else if(j == (inputarray.size() - 2)) {
                break;
            }
        }

        j = 1;
        for(int i = 0; i < inputarray.size(); i++) {
            if(inputarray.get(j) == "|") {
                inputarray.remove(j);
                inputarray.add(j, Integer.toString(Integer.parseInt(inputarray.get(j-1)) | Integer.parseInt(inputarray.get(j+1))));
                inputarray.remove((j-1));
                inputarray.remove((j+1));
            }
            if(j <= (inputarray.size() - 3)) {
                j += 2;
            }
            else if(j == (inputarray.size() - 2)) {
                break;
            }
        }

    }
    catch(Exception e) {
        e.printStackTrace();
        System.err.println("Invalid input entered, please try again");
    }
    finally {
        System.out.println();
        main(null);
    }
}
}

最佳答案

有几个问题。

  • 您似乎正在将单个字符串读入 ArrayList。这意味着 ArrayList 的大小为 1。因此 inputarray.get(1) 将产生您观察到的异常。您可能打算逐个字符地解析字符串,但这不是这里发生的情况。
  • 使用.equals()而不是==来比较字符串。这在 Stackoverflow 上已经讨论过很多次了。
  • 在迭代集合时向集合添加和删除项目是危险的,需要特别小心。也许你需要重新思考逻辑。

关于Java-获取 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30021100/

相关文章:

java - 为什么 jframe 最大化时会隐藏任务栏?

java - 使用 JDBC 将 Microsoft Access 数据库连接到 Java - 错误

Java索引越界异常与dijkstra算法

java - 线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException : 5

java - 存储压缩图像

java - 找不到 Web UI 的资源路径 : org/apache/spark/ui/static While creating a Spark application

java - SAXParseException;无效的编码名称 "UTF8"

android - IndexOutOfBoundsException 对我来说没有意义

java - ArrayIndexOutOfBoundsException 的原因

Java 数组索引越界