ArrayIndexOutOfBounds异常Java

时间:2015-02-05 15:43:09

标签: java indexoutofboundsexception

我似乎无法弄清楚它出了什么问题,或者为什么它给了我它的输出,我将复制&粘贴我的两个类的代码和我得到的控制台输出,也许在Java上比我更好的人可以帮助解决这个问题,我确定它是微不足道的: 头等

public class PoolTest {
    public static void main(String args[]){
        Pool newPool = new Pool();
        System.out.println(newPool.getValue('q'));
        System.out.println(newPool.getTiles(10));
        newPool.poolReset();
        System.out.println(newPool.getTiles(11));
        /* We have 100 tiles in the pool */
        System.out.println(newPool.getTilesRemaining()); //100
        System.out.println(newPool.isEmpty()); //false
        newPool.getTiles(5); //ok
        System.out.println(newPool.getTilesRemaining()); //95
    }
}

第二课

import java.util.Random;

public class Pool {
char[] zeroPoint = {'*','*'};
char[] onePoint = {'a','a','a','a','a','a','a','a','a','e','e','e','e','e','e','e','e','e','e','e','e','i','i','i','i','i','i','i','i','i','o','o','o','o','o','o','o','o','n','n','n','n','n','n','r','r','r','r','r','r','t','t','t','t','t','t','l','l','l','l','s','s','s','s','u','u','u','u'};
char[] twoPoint = {'d','d','d','d', 'g', 'g', 'g', 'g',};
char[] threePoint = {'b', 'b', 'c', 'c', 'm', 'm', 'p', 'p'};
char[] fourPoint = {'f', 'f', 'h', 'h', 'v', 'v', 'w', 'w', 'y', 'y'};
char[] fivePoint = {'k'};
char[] eightPoint = {'j', 'x'};
char[] tenPoint = {'q', 'z'};
char[] charPool = new char[102];
String stringPool = new String(onePoint)+ new String(twoPoint)+ new String(threePoint)+ new String(fourPoint)+ new String(fivePoint)+ new String(eightPoint)+ new String(tenPoint);
public Pool(){
    charPool = stringPool.toCharArray();
}

public int getValue(char letter){
    int value=0;
    if(new String(zeroPoint).indexOf(letter)>-1){
        value=0;
    } else if(new String(onePoint).indexOf(letter)>-1){
        value=1;
    } else if(new String(twoPoint).indexOf(letter)>-1){
        value=2;
    } else if(new String(threePoint).indexOf(letter)>-1){
        value=3;
    } else if(new String(fourPoint).indexOf(letter)>-1){
        value=4;
    } else if(new String(fivePoint).indexOf(letter)>-1){
        value=5;
    } else if(new String(eightPoint).indexOf(letter)>-1){
        value=8;
    } else if(new String(tenPoint).indexOf(letter)>-1){
        value=10;
    }

    return value;
}

public void poolReset(){
    charPool = stringPool.toCharArray();
}

public char[] getTiles(int numberOfTiles){
    Random randomTile = new Random();
    int randomNum;
    int returned;
    char[] tilesReturned = new char[numberOfTiles];

    for(int i=0; i<numberOfTiles; i++){
        returned=0;
        do{
            randomNum=randomTile.nextInt(102);
            if(charPool[randomNum]!='0') {
                tilesReturned[i]=charPool[randomNum];
                charPool[randomNum]='0';
                returned = 1;
            }
        }while(returned==0);
    }
    return tilesReturned;
}
public char[] swapTiles(char[] tilesToSwap){
    Random randomTile = new Random();
    int randomNum;
    int returned;
    int numberOfTiles = tilesToSwap.length;
    char[] tilesReturned = new char[numberOfTiles];

    for(int i=0; i<numberOfTiles; i++){
        returned=0;
        while(returned==0){
            randomNum=randomTile.nextInt(102);
            if(charPool[randomNum]!='0') {
                tilesReturned[i]=charPool[randomNum];
                charPool[randomNum]=tilesToSwap[randomNum];
                returned = 1;
            }
        }
    }
    return tilesReturned;
}

public boolean isEmpty(){
    boolean empty=true;
    for(int i=0; i<102; i++){
        if(charPool[i]!='0'){
            empty=false;
        }
    }
    return empty;
}

public int getTilesRemaining(){
    int tilesRemaining=0;
    for(int i=0; i<102; i++){
        if(charPool[i]=='0'){
            tilesRemaining++;
        }
    }
    return tilesRemaining;
}

}

控制台输出

10
stsfrebias
goresthltii
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 99
    at Pool.getTilesRemaining(Pool.java:98)
    at PoolTest.main(PoolTest.java:10)

5 个答案:

答案 0 :(得分:3)

使用

charPool = stringPool.toCharArray();

您创建一个新数组char[],其长度可能小于102.将循环条件更改为

for(int i=0; i < charPool.length; i++){

处理有效范围内的元素。

答案 1 :(得分:2)

这行代码更改了charPool的值,使其短于102。

String stringPool = new String(onePoint)+ new String(twoPoint)+ new String(threePoint)+ new String(fourPoint)+ new String(fivePoint)+ new String(eightPoint)+ new String(tenPoint)

然后你有这一行:

charPool = stringPool.toCharArray();

我相信stringPool的长度是99个字符,而不是102个。所以当你用这样的for循环迭代你的charPool时:

for(int i=0; i<102; i++){

它将&#34; outofindex&#34;。您可能希望将上述循环更改为:

for(int i=0; i<charPool.length; i++){

答案 2 :(得分:1)

charPool = stringPool.toCharArray()会将您分配的数组替换为一个显然只有98个字符的新数组。

如果您的目标是将字符串的字符复制到另一个字符数组中,请使用arrayCopy。

答案 3 :(得分:0)

getTiles()方法给出一串随机的tile,tile的数量取决于numbersOfTiles。但是在charPool中,当你使用一个char时,char将替换为'0',你不会再使用它。

poolReset()方法,你重置所有字符,所有'0'将被原始字符替换。

getTilesRemaing()方法,你从0索引开始循环,但charPool的长度只有99,所以你应该以99而不是102结束。 并且使用幻数进行循环是非常糟糕的主意。正如上面提到的朋友,你最好使用charPool.length。您可以通过这种方式保留异常。此功能为您提供了瓷砖数量。如果你的意思是字母是瓷砖,你应该改变:charPool [i] =='0'到charPool [i]!='0'或者返回charPool.length - tilesRemaing。

isEmpty()方法,它与getTilesRemaing()有同样的问题。 你永远无法达到指数102.

IDE的Debugger按钮永远是你最好的朋友!

答案 4 :(得分:0)

charPool成员没有按照您的想法进行初始化。

public Pool(){
  System.out.println("before: " + charPool.length);
  charPool = stringPool.toCharArray();
  System.out.println("after: " + charPool.length);
}

的产率:

before: 102
after: 99

简而言之,请使用数据结构中可用的方法(长度),并且不要将值(102)反复硬编码到代码中。