簡體   English   中英

Java 數組索引越界異常 (-1)?

[英]Java Array Index out of Bounds Exception (-1)?

我意識到以前有人問過這個問題,但我無法理解我閱讀的大部分答案。 我一直在研究這段代碼一段時間:

  static ArrayList<String> psalmsTitlesArray = new ArrayList<>(47);
    static ArrayList<String> psalmsNumbersArray = new ArrayList<>(47);
    static String psalmTextFileArray[] = new String[47];
    static int index = 0;

    public static void main(String[] args) throws IOException {
        //this program demonstrates a binary array search. It is going to search
        //the text file "Psalms.txt" for a number list of the pslams.
        // eg. the user wants to see psalm 7
        // the program will display "prayer of the virtuous under persecution".
        BufferedReader fileRead = new BufferedReader(new FileReader("Psalms.txt")); //create a BufferedReader to read the file
        String fileLine = "";
        int lineNumber = 1;
        for (int i = 0; i < 47; i++) {
            fileLine = fileRead.readLine(); // stores each line of text in a String 
            if (fileLine == null) { //if the line is blank
                break; // don't read it
            }
            psalmTextFileArray[i] += fileLine;
            if (lineNumber % 2 == 0) { //if the line is not an even number 
                psalmsTitlesArray.add(fileLine); //add the line to the Titles array
                lineNumber++;
            } else { //otherwise,
                psalmsNumbersArray.add(fileLine); //add it to the numbers array
                lineNumber++;
            }
        }
        String userInput = JOptionPane.showInputDialog(null, "What psalm would you like me to search for?", "Psalm Finder",
                JOptionPane.QUESTION_MESSAGE);
        if (userInput == null) {
            System.exit(0);
        }
        int numberInput = Integer.parseInt(userInput);

        binarySearch(psalmsNumbersArray, 0, (psalmsNumbersArray.size() - 1), userInput);
        for (int i = 0; i < psalmTextFileArray.length; i++) {
        index = psalmTextFileArray[i].indexOf(numberInput);
        }
        JOptionPane.showMessageDialog(null, "I found Psalm #" + userInput + ". It is: \n" + psalmTextFileArray[index]);

    }

    public static boolean binarySearch(ArrayList<String> myPsalmsArray, int left, int right, String searchForPsalm) {
        int middle;
        if (left > right) {
            return false;
        }
        middle = (left + right) / 2;
        if (myPsalmsArray.get(middle).equals(searchForPsalm)) {
            return true;
        }
        if (searchForPsalm.compareTo(myPsalmsArray.get(middle)) < 0) {
            return binarySearch(myPsalmsArray, left, middle - 1,
                    searchForPsalm);
        } else {
            return binarySearch(myPsalmsArray, middle + 1, right,
                    searchForPsalm);
        }
    }
}

此代碼從文件“Psalm.txt”中讀取。 本質上,Pslams 是按 1 - 99 的順序排列的(但不是每個 Psalm 都包括在內,例如,文本文件中不存在 pslam 4)

我正在嘗試使用 indexOf() 來查找字符第一次出現在哪個索引處。 然后,我將能夠將它在數組中向前移動一個索引並找到標題,因為數組中的信息如下所示:

[2, 詩篇 2 標題, 3, 詩篇 3 標題, 4...] (等)

這是似乎導致問題的一小段代碼:

for (int i = 0; i < psalmTextFileArray.length; i++) {
            index = psalmTextFileArray[i].indexOf(numberInput);
            }
            JOptionPane.showMessageDialog(null, "I found Psalm #" + userInput + ". It is: \n" + psalmTextFileArray[index]);

所以,如果我能在數組中找到 2 的索引,將它向前移動一個索引,我就會有標題。 此外,索引始終拋出 -1 異常。 這是什么意思?

抱歉,如果這令人困惑,我可以澄清任何不清楚的地方(我會盡力而為!)

由於您的代碼無效,您收到異常

 int numberInput = Integer.parseInt(userInput);

    binarySearch(psalmsNumbersArray, 0, (psalmsNumbersArray.size() - 1), userInput);
    for (int i = 0; i < psalmTextFileArray.length; i++) {
    index = psalmTextFileArray[i].indexOf(numberInput);
    }
    JOptionPane.showMessageDialog(null, "I found Psalm #" + userInput + ". It is: \n" + psalmTextFileArray[index]);

在上面的代碼中psalmTextFileArray[i]將返回一個String對象。 那么你正在做indexOf(numberInput)

JavaDoc

此對象表示的字符序列中該字符第一次出現的索引,如果該字符沒有出現,則為 -1。

因此,如果您的numberInput不在String對象中,您的index將變為 -1

index = -1

在下一行中,當您調用showMessageDialog時,您正在使用-1訪問數組的位置,因為您正在使用psalmTextFileArray[index]因此您得到ArrayIndexOutOfBoundsException

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM