私はポケモン型のマッチアップ電卓を作っています(ウォータービートなど)、私はかなり奇妙なバグに遭遇しました。私はそのポケモンのすべての弱点を含むArrayListを繰り返し、それらのどれかが打ち消されているかどうかを見るためにそれらをすべての抵抗と比較しています。 Here's GitHub上のリポジトリ。例外はsrc/main/Pokemon.javaの140行目にスローされています。アイテムが削除されたときにインデックスが変更されたが、まだ運がないことを説明しました。問題のループは次のとおりです。Java ArrayList ArrayIndexOutOfBoundsException
//Check 2x weakness v 2x Resist
eDiff = 0;
rDiff = 0;
for (int e = 0; e < effective.size()-eDiff; e++) {
for (int r = 0; r < resist.size()-rDiff; r++) {
if (effective.get(e-eDiff).equals(resist.get(r-rDiff))) {
effective.remove(e-eDiff);
resist.remove(r-rDiff);
eDiff++;
rDiff++;
}
}
}
http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Tunaki