ArrayIndexOutOfBounds 异常 Java
ArrayIndexOutOfBounds Exception Java
我似乎无法弄清楚出了什么问题或为什么它会给我它的输出,我将复制并粘贴我的两个 classes 代码和我得到的控制台输出,也许有人在 Java 比我好可以帮助解决这个问题,我确定这是微不足道的事情:
第一个class
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
}
}
秒class
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)
charPool = stringPool.toCharArray()
将用一个显然只有 98 个字符的新数组替换您分配的数组。
如果您的目标是将字符串中的字符复制到另一个字符数组中,请使用 arrayCopy。
有
charPool = stringPool.toCharArray();
您创建了一个新数组 char[]
,其长度可能小于 102。将循环条件更改为
for(int i=0; i < charPool.length; i++){
处理有效范围内的元素。
您的这行代码更改了 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++){
会"outofindex"。您可能希望将上述循环更改为:
for(int i=0; i<charPool.length; i++){
getTiles() 方法给出一串随机图块,图块的数量取决于numbersOfTiles。但是在charPool中,当你使用一个char时,该char将被替换为'0',你将不会再使用它。
poolReset()方法,你重置所有字符,所有'0'将被原来的字符替换。
getTilesRemaing()方法,你从0索引开始循环,但是charPool的长度只有99,所以你应该以99结束而不是102。
在循环中使用幻数是非常糟糕的主意。上面的朋友说了,你最好用charPool.length。您可以通过这种方式远离异常。这个函数给你瓷砖的数量。如果你的意思是字母是瓷砖,你应该改变: charPool[i]=='0' 到 charPool[i]!='0' 或 return charPool.length - tilesRemaing.
isEmpty()方法,和getTilesRemaing()有同样的问题。
你永远无法到达索引 102。
IDE 的调试器按钮永远是您最好的朋友!
charPool 成员没有像您想象的那样被初始化。
public Pool(){
System.out.println("before: " + charPool.length);
charPool = stringPool.toCharArray();
System.out.println("after: " + charPool.length);
}
产量:
before: 102
after: 99
简而言之,使用数据结构上可用的方法(长度),不要将值 (102) 重复硬编码到代码中。
我似乎无法弄清楚出了什么问题或为什么它会给我它的输出,我将复制并粘贴我的两个 classes 代码和我得到的控制台输出,也许有人在 Java 比我好可以帮助解决这个问题,我确定这是微不足道的事情:
第一个class
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
}
}
秒class
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)
charPool = stringPool.toCharArray()
将用一个显然只有 98 个字符的新数组替换您分配的数组。
如果您的目标是将字符串中的字符复制到另一个字符数组中,请使用 arrayCopy。
有
charPool = stringPool.toCharArray();
您创建了一个新数组 char[]
,其长度可能小于 102。将循环条件更改为
for(int i=0; i < charPool.length; i++){
处理有效范围内的元素。
您的这行代码更改了 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++){
会"outofindex"。您可能希望将上述循环更改为:
for(int i=0; i<charPool.length; i++){
getTiles() 方法给出一串随机图块,图块的数量取决于numbersOfTiles。但是在charPool中,当你使用一个char时,该char将被替换为'0',你将不会再使用它。
poolReset()方法,你重置所有字符,所有'0'将被原来的字符替换。
getTilesRemaing()方法,你从0索引开始循环,但是charPool的长度只有99,所以你应该以99结束而不是102。 在循环中使用幻数是非常糟糕的主意。上面的朋友说了,你最好用charPool.length。您可以通过这种方式远离异常。这个函数给你瓷砖的数量。如果你的意思是字母是瓷砖,你应该改变: charPool[i]=='0' 到 charPool[i]!='0' 或 return charPool.length - tilesRemaing.
isEmpty()方法,和getTilesRemaing()有同样的问题。 你永远无法到达索引 102。
IDE 的调试器按钮永远是您最好的朋友!
charPool 成员没有像您想象的那样被初始化。
public Pool(){
System.out.println("before: " + charPool.length);
charPool = stringPool.toCharArray();
System.out.println("after: " + charPool.length);
}
产量:
before: 102
after: 99
简而言之,使用数据结构上可用的方法(长度),不要将值 (102) 重复硬编码到代码中。