1

我目前有以下代码。我在这一行得到 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);
}   
4

2 回答 2

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];
            }
        }
    }
于 2012-07-11T01:49:28.843 回答
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 = 2dta[3] = dta[2]超出了dta.

正如安息日所建议的,您需要纠正for循环。但是,我认为您可能会遗漏更多的东西来让它做您想做的事。除其他外,还有一个事实,即dta[flagcounter] = dta[x];不会将x行中的值分配给flagcounter行,而是实际上通过引用使flagcounter指向行。x

于 2012-07-11T01:58:53.933 回答