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字符串数组初始化循环结构内外

java - IntelliJ理念14 : How do I import all existing idea modules in a folder tree into a project

java - 比较两个不同类型的数组,打印相同的元素 "slot"Java

Java JFrame在不可见后使用.setVisible(true)后不会显示

string - 如何找到字符串的周期

c - 如何检查字符串是否为数字?

java - 删除字符串中某个位置的字符

java - Spring 启动: Error creating bean with name 'springSecurityFilterChain'

java - "java.net.BindException: Can' t 分配请求地址 "on client' s 端

java - 解析门户 URL