java.lang.IndexOutOfBoundsException: Index 7, Size:7

情到浓时终转凉″ 提交于 2019-12-20 02:06:49

问题


I am creating a program that finds the solutions to the eight queens problem by breadth search. My code so far:

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

This compiles without any errors, but when I go to run the program I get this error:

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)

How can I fix this?


回答1:


Java uses 0 for the first index, not one. Adjust your program accordingly and it will work. If you have 7 items in an array you use 0-6, not 1-7.

Also, use descriptive names for your variables. a, b, m, x, etc are just bad.




回答2:


The problem is the following:

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

You are iterating over your b list and probably remove an element from c when n is 7, so you end up with only 7 elements in c and therefore an OutOfBoundsException.

What you wanna do is to add a break that the iteration stops once so you have deleted the element:

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

This should fix it.



来源:https://stackoverflow.com/questions/20379351/java-lang-indexoutofboundsexception-index-7-size7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!