好吧,我正在寫多米諾骨牌般的程序,我在其中添加,移除,旋轉瓷磚以將它們放置在棋盤上等等。也許我錯過了一些基本上簡單的東西,但我不能爲我的生活找出爲什麼當我試圖從董事會中刪除一塊瓷磚,我得到一個IndexOUtOfBoundsException。我已經手動追蹤了代碼,使用了以前從未使用過的調試工具,並且所有的值都是正確的。ArrayList IndexOutOfBoundsException Java
以下是我使用的瓷磚添加到板的方法之一:
public boolean placePieceAfter(int tile[])
{
if (tile[1] == Board.board.get(Board.board.size() - 1))
{
for (int index = 0; index < 4; index = index + 1)
Board.board.add(tile[index]);
return true;
}
else
{
System.out.println("\n" + red + "You cannot place this piece last.");
return false;
}
}
以下是我使用的是看我可以插入一個給定的play()方法從玩家/計算機手中的瓦片(在前面,或者在匹配的瓦片之間或之後)插入板中。我假設當我引用if語句中的方法時,它們被執行(我是否正確?)。我還放置了System.out.println語句來查看我正在使用的變量的值。
public boolean play()
{
int firstElement = 0;
int secondElement = 0;
int thirdElement = 0;
int fourthElement = 0;
for (int checkEveryTile = 0; checkEveryTile < computerHand.size(); checkEveryTile = checkEveryTile + 4)
{
computerHand.size();
System.out.println("Computer hand: " + computerHand);
for (int elementInEachTile = checkEveryTile; elementInEachTile < (elementInEachTile + 4); elementInEachTile = elementInEachTile + 1)
{
boolean gatheredElements = false;
while (!gatheredElements)
{
firstElement = computerHand.get(elementInEachTile);
secondElement = computerHand.get(elementInEachTile + 1);
thirdElement = computerHand.get(elementInEachTile + 2);
fourthElement = computerHand.get(elementInEachTile + 3);
gatheredElements = true;
}
int tileToTestFor[] = {firstElement, secondElement, thirdElement, fourthElement};
// int elementToAddToTile = computerHand.get(index);
//elementToAddToTile = first element in computerHand
// for (int addingToTileToTestFor = 0; addingToTileToTestFor < 4; addingToTileToTestFor = addingToTileToTestFor + 1)
// tileToTestFor[addingToTileToTestFor] = elementToAddToTile;
//tileToTest for gets first element of computerHand
if (placePieceBefore(tileToTestFor))
{
System.out.println("Location of checkEveryTile (Index): " + checkEveryTile);
System.out.println("Element at checkEveryTile: " + computerHand.get(checkEveryTile));
for (int indexToRemoveFrom = elementInEachTile; indexToRemoveFrom < (indexToRemoveFrom + 4); indexToRemoveFrom = indexToRemoveFrom + 1)
computerHand.remove(checkEveryTile);
Board.printBoard();
return true;
}
else if (placePieceBetween(tileToTestFor))
{
System.out.println("Location of checkEveryTile (Index): " + checkEveryTile);
System.out.println("Element at checkEveryTile: " + computerHand.get(checkEveryTile));
for (int indexToRemoveFrom = elementInEachTile; indexToRemoveFrom < (indexToRemoveFrom + 4); indexToRemoveFrom = indexToRemoveFrom + 1)
computerHand.remove(checkEveryTile);
Board.printBoard();
return true;
}
else if (placePieceAfter(tileToTestFor))
{
System.out.println("Location of checkEveryTile (Index): " + checkEveryTile);
System.out.println("Element at checkEveryTile: " + computerHand.get(checkEveryTile));
for (int indexToRemoveFrom = elementInEachTile; indexToRemoveFrom < (indexToRemoveFrom + 4); indexToRemoveFrom = indexToRemoveFrom + 1)
computerHand.remove(checkEveryTile);
Board.printBoard();
return true;
}
else
break;
}
}
System.out.println("\n" + red + "Computer could not play.");
return false;
}
這裏與我得到錯誤的輸出(它總是在playerHand.remove(checkEveryTile)
或computerHand.remove(checkEveryTile)
語句的if語句內):
Pile before players get their respective hands.
Pile: [5, 6, 7, 8, 0, 9, 4, 2, 2, 1, 4, 7, 9, 9, 5, 5, 7, 0, 3, 6, 4, 8, 2, 3, 4, 4, 2, 3, 5, 4, 1, 8, 9, 9, 0, 7, 4, 6, 6, 3, 2, 8, 6, 5, 0, 0, 1, 8, 8, 6, 0, 4, 6, 2, 7, 8, 9, 6, 7, 4, 0, 6, 9, 2, 6, 8, 2, 3, 3, 4, 8, 5, 3, 0, 7, 5, 4, 4, 9, 3, 6, 5, 0, 4, 3, 4, 1, 9, 1, 8, 6, 1, 7, 4, 2, 0, 6, 2, 2, 7, 8, 8, 7, 0, 5, 0, 8, 1, 6, 9, 3, 2, 6, 1, 0, 0, 5, 8, 0, 9, 3, 0, 1, 6, 7, 1, 4, 6, 0, 6, 3, 5, 0, 4, 6, 1, 0, 5, 8, 2, 6, 0, 7, 1, 3, 9, 5, 2, 3, 4, 7, 0, 5, 8, 3, 1, 3, 1, 0, 6, 3, 4, 3, 9, 6, 5, 5, 7, 2, 3, 4, 5, 9, 1, 0, 5, 1, 8, 4, 6, 5, 7, 2, 2, 1, 5, 0, 0, 0, 3, 1, 9, 1, 5, 4, 6, 0, 2, 8, 5, 2, 8, 2, 4, 0, 8, 8, 4]
This is the player hand.
Player hand: [5, 6, 7, 8, 0, 9, 4, 2, 2, 1, 4, 7, 9, 9, 5, 5, 7, 0, 3, 6]
This is the computer hand.
Computer hand: [4, 8, 2, 3, 4, 4, 2, 3, 5, 4, 1, 8, 9, 9, 0, 7, 4, 6, 6, 3]
Pile after players get their respective hands.
Pile: [2, 8, 6, 5, 0, 0, 1, 8, 8, 6, 0, 4, 6, 2, 7, 8, 9, 6, 7, 4, 0, 6, 9, 2, 6, 8, 2, 3, 3, 4, 8, 5, 3, 0, 7, 5, 4, 4, 9, 3, 6, 5, 0, 4, 3, 4, 1, 9, 1, 8, 6, 1, 7, 4, 2, 0, 6, 2, 2, 7, 8, 8, 7, 0, 5, 0, 8, 1, 6, 9, 3, 2, 6, 1, 0, 0, 5, 8, 0, 9, 3, 0, 1, 6, 7, 1, 4, 6, 0, 6, 3, 5, 0, 4, 6, 1, 0, 5, 8, 2, 6, 0, 7, 1, 3, 9, 5, 2, 3, 4, 7, 0, 5, 8, 3, 1, 3, 1, 0, 6, 3, 4, 3, 9, 6, 5, 5, 7, 2, 3, 4, 5, 9, 1, 0, 5, 1, 8, 4, 6, 5, 7, 2, 2, 1, 5, 0, 0, 0, 3, 1, 9, 1, 5, 4, 6, 0, 2, 8, 5, 2, 8, 2, 4, 0, 8, 8, 4]
Board before anyone places a piece.
Board: []
Board: [5]
Board: [5, 6]
Board: [5, 6, 7]
Board: [5, 6, 7, 8]
Player placed initial tile.
Computer hand: [4, 8, 2, 3, 4, 4, 2, 3, 5, 4, 1, 8, 9, 9, 0, 7, 4, 6, 6, 3]
Piece: [4, 8, 2, 3]
You cannot place this piece in the beginning.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
You cannot place this piece in the middle.
Location of checkEveryTile (Index): 0
Element at checkEveryTile: 4
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.remove(ArrayList.java:445)
at numbertiles.Computer.play(Computer.java:172)
at numbertiles.Board.takeTurns(Board.java:85)
at numbertiles.NumberTiles.main(NumberTiles.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
這個問題的任何幫助,一定會理解。謝謝。
這是很多需要經歷的代碼。將其縮小到與您的錯誤相關的部分。 – Prateek
根據堆棧跟蹤,錯誤發生在Computer.java第172行。在那裏設置一個斷點,並確保沒有刪除超出'ArrayList'範圍的索引。 –
在此處發佈任何內容之前_please_在調試器中逐句通過代碼。 99%的時間會以這種方式發現錯誤。如果您仍有問題,請將其降至最低[SSCCE](http://sscce.org)。 –