java - 使用 String 嵌套 for 循环

标签 java string

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

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

我总是遇到同样的异常:

StringIndexOutOfBoundsException: String index out of range: 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”,在这种情况下 in.length=100 现在看看你的代码会发生什么:(阅读评论)

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
                    }
                }
            }

在这里,在代码中变量的值 i不必要地增加。现在想想 i=99在循环。现在看看您的代码中发生了什么:(仔细阅读评论):

    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++;
                        }
                    }
                }

我想你明白为什么会出现错误 StringIndexOutOfBoundsException: String index out of range: 100发生。

要改进您的代码,您可以使用 else if阶梯并且不递增i不必要因为i是 for 循环中的增量 - for(int i=0;i<in.length();i++ .并在开始编码之前尝试设置/清除逻辑。

关于java - 使用 String 嵌套 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29609104/

相关文章:

java - 获取包(jar)中具有特定后缀的所有资源

java - 在初始化之前打印字段似乎在初始化之后打印它

java - HTML 5 视频标签不适用于 Tomcat(已编辑)

ios - Xcode 错误 - 文件中缺少所需的架构 i386

python - 在 Python 中标记一个保留分隔符的字符串

java - 更改 JPanel java 中形状列表的颜色

Java:小游戏的可变计时器

javascript - 在 Javascript 中获取变量的名称和字符串

sql - 自动截断分配的字符串到字段的长度?

字符串.替换器 : how to replace all substrings at once?