Java数组索引超出范围异常排序

时间:2012-07-11 01:32:33

标签: java

我目前有以下代码。我在这一行得到了ArrayIndexOutofBoundsException。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Module3_1_Sort.sort(Module3_1_Sort.java:70)
at Module3_1.s_2d_string(Module3_1.java:155)

相应的行如下。

dta[flagcounter] = dta[x];
sortValues = s.sort(sortValues,counter, sortBy, searchterm);

我很确定它不会超过数组的长度......

有人可以帮忙吗?谢谢!

程序代码如下

    static public void s_2d_string () {
    c.println("2D String Array Program");
    int counter,x;
    c.print("How many entries do you wish to sort? ");
    counter = c.readInt();

    String[][] sortValues = new String[counter+1][2];

    for (x=0;x<counter;x++) {
        c.print("Enter book name: ");
        sortValues[x][0] = c.readLine();
        c.print("Enter book author: ");
        sortValues[x][1] = c.readLine();

    }
    c.print("Which column would you like to sort by? 1 or 2? ");
    int sortBy = c.readInt();
    sortBy = sortBy-1;

    c.print("Enter search term: ");
    String searchterm = c.readLine();

    sortValues = s.sort(sortValues,counter, sortBy, searchterm);

    int flagcounter_int = Integer.parseInt(sortValues[0][0]);

    c.println(flagcounter_int + " results found.");

    for (x=0;x<flagcounter_int;x++) {
        c.println(sortValues[x+1][0] + ", " + sortValues[x+1][1]);
    }
}


static public String[][] sort (String dta[][], int totalNo, int sortBy, String searchterm)  {
    boolean found = false;    
    int flagcounter = 0;
        for (int x=0; x<dta.length;x++) {
            if (sortBy == 0) {
                if (searchterm.equalsIgnoreCase(dta[x][0])) {
                    found = true;
                    flagcounter = flagcounter+1;
                    dta[flagcounter] = dta[x];
                }
            }
            if (sortBy == 1) {
                if (searchterm.equalsIgnoreCase(dta[x][1])) {
                    found = true;
                    flagcounter = flagcounter+1;
                    dta[flagcounter] = dta[x];
                }
            }
        }
    String flagcounter_string = Integer.toString(flagcounter);
    dta[0][0] = flagcounter_string;
    return (dta);
}   

2 个答案:

答案 0 :(得分:2)

更改您的代码
for (int x=0; x<dta.length;x++) {
        if (sortBy == 0) {
            if (searchterm.equalsIgnoreCase(dta[x][0])) {
                found = true;
                flagcounter = flagcounter+1;
                dta[flagcounter] = dta[x];
            }
        }
        if (sortBy == 1) {
            if (searchterm.equalsIgnoreCase(dta[x][1])) {
                found = true;
                flagcounter = flagcounter+1;
                dta[flagcounter] = dta[x];
            }
        }
    }

for (int x=0; x<totalNo;x++) {
        if (sortBy == 0) {
            if (searchterm.equalsIgnoreCase(dta[x][0])) {
                found = true;
                flagcounter = flagcounter+1;
                dta[flagcounter] = dta[x];
            }
        }
        if (sortBy == 1) {
            if (searchterm.equalsIgnoreCase(dta[x][1])) {
                found = true;
                flagcounter = flagcounter+1;
                dta[flagcounter] = dta[x];
            }
        }
    }

答案 1 :(得分:2)

查看for循环以及您在其中处理flagcounter的方式。 for循环说:

for (int x = 0; x < dta.length; x++)

并且在您将搜索词用作索引之前,flagcounter会增加:

flagcounter = flagcounter + 1;
dta[flagcounter] = dta[x];

如果您在第一行中有匹配项,那么您将继续在下一行中找到它(因为您覆盖了它)并且最终超出范围。

让我们看一个例子。假设您将此作为输入:

Book            Author
-----------------------------
Proven Guilty   Jim Butcher
Naked Sun       Isaac Asimov

所以dta将是这样的:

[
    ["Proven Guilty", "Jim Butcher"],
    ["Naked Sun", "Isaac Asimov"],
    [null, null]
]

假设您正在寻找作者“Jim Butcher”。因此,当您进入循环时,您有x = 0, flagcounter = 0。您立即找到匹配项,结果如下:

flagcounter = flagcounter + 1; // flagcounter is now 1
dta[flagcounter] = dta[x]; // i.e. dta[1] = dta[0];

所以现在dta数组看起来像这样:

[
    ["Proven Guilty", "Jim Butcher"],
    ["Proven Guilty", "Jim Butcher"],
    [null, null]
]

您可以看到会发生什么:您继续将上一行分配到下一行,最后您处于x = 2flagcounter = 2并且您尝试执行dta[3] = dta[2]的情况,超出dta的范围。

正如安息日所说,你需要纠正for循环。但是,我认为你可能会遗漏更多东西,让它做你想做的事。除此之外,事实是dta[flagcounter] = dta[x];没有将x行中的值分配给flagcounter行,但实际上使flagcounter指向x行引用。