索引超出界限Java索引:3,大小:3

时间:2015-05-19 04:08:07

标签: java arrays for-loop while-loop stringbuilder

大家好,在制作单词拼图游戏的时候,我遇到了一个双重检查的问题,纵向和横向都是我当前文件中保存为arraylist的单词。

 String letterSize = "" + size;
    makeLetterWordList(letterSize);
    boolean finished = false;
    while ( !finished ) {
        finished = true;
        for (int i = 0; i < size; i++) {
            int randomYWord = randomInteger(wordList.size());
            String item = wordList.get(randomYWord);
            puzzleListY.add(item);
        }
        for (int i = 0; i <= puzzleListY.size(); i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j <= puzzleListY.size(); j++) {
                sb.append(puzzleListY.get(j).charAt(j));
            }
            randomXWord = sb.toString();
            if (!wordList.contains(randomXWord)) {
                finished = false;
                break;
            } else {
                puzzleListX.add(randomXWord);
            }

        }
    }

产生的错误如下:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.rangeCheck(ArrayList.java:653)

我很难在我的代码中发现错误,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

循环应该只有大小 - 1

@Local
public interface AccountServiceLocal {

    public void create(Account a);

    public void delete(Object id);

    public Account find(Object id);

    public List<?> findemail(Object id);

    public void update(Account a);

}

对于其他循环,因为你从索引0开始并且有n个元素循环应该是从0到n-1