我在使用此方法时遇到问题。

它是一个带有2个参数的构造函数:一个名称和一个带有100个字符的字符串,称为inin必须放入其中包含vak(一个对象)的array [10] [10]。

我总是得到同样的异常:


  StringIndexOutOfBoundsException:字符串索引超出范围:100


这是方法:

 public SpelBord(String naam, String in) {
        int muur=1;
        this.naam = naam;
        System.out.println(in.length());
         for(int i=0;i<in.length();i++) {
            for (int j = 0; j < vakken.length; j++) {
                for (int k = 0; k < vakken[j].length; k++) {

                    if (in.charAt(i) == '.') {
                        this.vakken[j][k] = new Vak();

                    }
                    if (in.charAt(i) == 'K') {
                        this.vakken[j][k] = new Vak(false, false, new   Kist());
                    }
                    if (in.charAt(i) == 'D') {
                        this.vakken[j][k] = new Vak(true, false);

                    }
                    if (in.charAt(i) == 'M') {
                        this.vakken[j][k] = new Vak(false, false, new Man());



                    }
                     if (in.charAt(i) == '#') {
                        this.vakken[j][k] = new Vak(false, true);

                    }
                }
            }
        }


我认为这与for循环有关。
先感谢您!

最佳答案

您收到此错误StringIndexOutOfBoundsException: String index out of range: 100

因为:
在这种情况下,假设您输入的字符串为100个字符,并且该字符串为“ ABCDEFGHIJK ........ LMNOPQRSTUVWXYZ”
现在查看代码发生了什么:(阅读注释)

for(int i=0;i<in.length();i++) {// i=0
            for (int j = 0; j < vakken.length; j++) {
                for (int k = 0; k < vakken[j].length; k++) {

                    if (in.charAt(i) == '.') {//looking for charAt(0)
                        this.vakken[j][k] = new Vak();
                i++;//if this condition satisfies i=1
                    }
                    if (in.charAt(i) == 'K') {//if last if condition satisfies looking for charAt(1)
                        this.vakken[j][k] = new Vak(false, false, new Kist());
                i++;//if this condition satisfies i=2
                    }
                    if (in.charAt(i) == 'D') {//if last if condition satisfies looking for charAt(2)
                        this.vakken[j][k] = new Vak(true, false);
                i++;//if this condition satisfies i=3
                    }
                    if (in.charAt(i) == 'M') {//if last if condition satisfies looking for charAt(3)
                        this.vakken[j][k] = new Vak(false, false, new Man());

                                 muur++;
                i++;//if this condition satisfies i=4
                    }
                     if (in.charAt(i) == '#') {//if last if condition satisfies looking for charAt(4)
                        this.vakken[j][k] = new Vak(false, true);
            i++;//if this condition satisfies i=5
                    }
                }
            }


在此,代码中变量in.length=100的值不必要地增加。现在认为循环中的i。现在查看代码中发生了什么:(请仔细阅读注释):

    for(int i=0;i<in.length();i++) {// i=99
                for (int j = 0; j < vakken.length; j++) {
                    for (int k = 0; k < vakken[j].length; k++) {

                        if (in.charAt(i) == '.') {//looking for charAt(99)
                            this.vakken[j][k] = new Vak();
                    i++;//if this condition satisfies i=100
                        }
                        if (in.charAt(i) == 'K') {
                        //if last if condition satisfies
                        //looking for charAt(100)
                        //now charAt(100) dose not exists
                        //and an error occurs that String out of range
                            this.vakken[j][k] = new Vak(false, false, new Kist());
                    i++;
                        }
                        if (in.charAt(i) == 'D') {
                            this.vakken[j][k] = new Vak(true, false);
                    i++;
                        }
                        if (in.charAt(i) == 'M') {
                            this.vakken[j][k] = new Vak(false, false, new Man());

                                     muur++;
                    i++;
                        }
                         if (in.charAt(i) == '#') {
                            this.vakken[j][k] = new Vak(false, true);
                i++;
                        }
                    }
                }


我认为您了解为什么会出现错误i=99

为了改善代码,您可以使用StringIndexOutOfBoundsException: String index out of range: 100梯形图,并且不必不必要地增加else if,因为i是for循环中的-cc增量。并尝试在开始编码之前设置/清除逻辑。

09-10 08:24
查看更多