Java Array Index Out of Bounds Exception表示数组小于实际数组

时间:2015-06-12 23:38:45

标签: java arrays

我的程序正在比较两个for循环中的两个字符串。运行时,它会产生错误:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2,即使数组已初始化足够的空间。代码行是:

if(keywords[a].equals(tokenizedString[b])&&tokenizedString[a].equals("1"))

初始化keywords的位置:

String[] keywords={"0","add","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"};

初始化tokenizedString的位置:

String[] tokenizedString={"0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"};

为什么,当我运行它时,是否说当两个数组在该语句中使用的数组明显更大时,两个超出界限?

代码的相关部分:

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        final int KEYWORDS = 3;
        String[] keywords =
                        {"0", "add", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
                                        "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
                                        "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
                                        "0", "0", "0", "0", "0", "0", "0"};
        Scanner scan = new Scanner(System.in);
        String[] tokenizedString =
                        {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
                                        "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
                                        "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
                                        "0", "0", "0", "0", "0", "0"};
        int stringCounter = 1;
        String input = scan.nextLine();
        StringTokenizer st = new StringTokenizer(input, " ");
        String answer = null;
        while (st.hasMoreElements()) {
            answer = (String) st.nextElement();
            System.out.println(answer);
            tokenizedString[stringCounter] = answer;
            stringCounter++;
        }
        String[] functions;
        functions = new String[10];
        functions = testForKeywords(tokenizedString, keywords, stringCounter, KEYWORDS);
        int[] numbers = new int[10];
        String[] numberss = {"1", "2"};
        numbers = testForNumbers(tokenizedString, numberss, stringCounter, 2);
    }

    public static int[] testForNumbers(String[] tokenizedString, String[] keywords, int inputs,
                    int numbKeywords) {
        int[] functions = new int[inputs + 1];
        int funCounter = 0;
        for (int a = 0; a <= numbKeywords; a++) {
            for (int b = 0; b <= inputs; b++) {
                if (keywords[a].equals(tokenizedString[b]) && tokenizedString[a].equals("1")) {
                    System.out.println("Yay");
                    functions[funCounter] = 1;
                    funCounter++;
                }
            }
        }
        return functions;
    }
}

1 个答案:

答案 0 :(得分:1)

在发生错误时,您对keywords的值感到困惑。

您的错误发生在testForNumbers(),其中您将本地(方法范围)变量keywords作为参数传入。如果你看到你称之为的地方,你可以在这个位置传递numberss

numberss只有两个成员 - 因此只有索引01 - 尝试访问索引2确实会在运行时抛出此异常