【问题标题】:java.lang.IndexOutOfBoundsException: Index 7, Size:7java.lang.IndexOutOfBoundsException:索引 7,大小:7
【发布时间】:2013-12-21 04:11:38
【问题描述】:

我正在创建一个程序,通过广度搜索找到八皇后问题的解决方案。到目前为止我的代码:

import java.util.*;
import java.lang.*;
import java.io.*;  

public class EightQueens {
public static void main(String[ ] args) {

    ArrayList<List<Integer>> states = new ArrayList<List<Integer>>();
    List<Integer> start=new ArrayList<Integer>();

    for (int s=0; s<8; s++) {
        start.add(0);
    }
    states.add(start);

    List<Integer> a = new ArrayList<Integer>();
    List<Integer> b = new ArrayList<Integer>();

    for (int j=1; j<9; j++) {

        a = states.get(0);
        states.remove(0);

        if (j==1) {

            for (int n=1; n<9; n++) {
                a.set(0,n);
                states.add(a);  
            }
        }

        else {

            for (int i=j-1; i>0; i--) {
                b.add(a.get(i-1));
                b.add(a.get(i-1)-1);
                b.add(a.get(i-1)+1);
            }

            for (int n=0; n<8; n++) {
                List<Integer> c = new ArrayList<Integer>();

                for (int t=1; t<9; t++) {
                    c.add(t);
                }

                for (int k=0; k<b.size(); k++) {
                    if (c.get(n)== b.get(k)) {
                        c.remove(n);
                    }
                }
                for (int r=0; r<c.size(); r++) {
                    if (c.get(r)==n+1) {
                        a.set(j-1,n+1);
                        states.add(a);
                    }
                }
            }
        }


    }

    for (int m=0; m< states.size(); m++) {
        a = states.get(0);
        for (int p=0; p< a.size(); p++) {
            int q = a.get(p);
            System.out.print(q);
        }
        states.remove(a);
        System.out.println(" ");
    }

    int numsol = states.size();
    System.out.println(numsol);
}
}

这编译没有任何错误,但是当我去运行程序时,我得到了这个错误:

Exception in thread "main" java.lang.IndexOutofBoundsException: Index: 7, Size: 7
   at java.util.ArrayList.rangeCheck(ArrayList.java:635)
   at java.util.ArrayList.get(Arraylist.java:411)
   at EightQueens.main(EightQueens.java:48)

我该如何解决这个问题?

【问题讨论】:

  • 你认为这个错误意味着什么?你试过调试吗?
  • 看看它给你的错误信息:Index: 7, Size: 7 现在请记住,索引编号从 0 开始,你应该找出问题所在!
  • 那是您的确切代码吗?您的异常告诉您的行对应于您在上面发布的代码?

标签: java exception arraylist indexoutofboundsexception


【解决方案1】:

问题如下:

if (c.get(n)== b.get(k)) {
    c.remove(n);
}

您正在遍历您的 b 列表,并且可能在 n 为 7 时从 c 中删除一个元素,因此您最终在 c 中只有 7 个元素,因此会出现 OutOfBoundsException。

你想要做的是添加一个break,迭代停止一次,这样你就删除了元素:

if (c.get(n)== b.get(k)) {
    c.remove(n);
    break;
}

这应该可以解决它。

【讨论】:

    【解决方案2】:

    Java 使用 0 作为第一个索引,而不是一个。相应地调整您的程序,它将起作用。如果数组中有 7 个项目,则使用 0-6,而不是 1-7。

    另外,为变量使用描述性名称。 a、b、m、x 等都不好。

    【讨论】:

    • 其实如果你看导致错误的那一行,他访问了c的8个元素(从0开始),但他之前也加了8个
    • 也许它来自反编译。谁在他们的头脑中会使用字母作为变量名称?!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多