GDX项目中Bullet类迭代器嵌套使用报错求助
Got it, let's break down this error you're hitting. The #iterator() cannot be used nested exception is super common when working with LibGDX's Array class, and it's easy to fix once you understand why it's happening.
Why This Happens
LibGDX's Array uses a fail-fast iterator that doesn't support nested iteration over the same array. This means you can't:
- Use a
for-eachloop (which under the hood uses an iterator) inside anotherfor-eachloop on the sameArray - Use an explicit iterator while another iterator (even an implicit one from a
for-eachloop) is active on the same array - Modify the array's size (via
add(),remove(), etc.) while iterating with an iterator, unless you use the iterator's ownremove()method
Looking at your stack trace, the error fires in PlayState.update() at line 53—so that's where your nested iteration or unsafe array modification is happening.
Solutions to Fix It
1. Replace Iterators with Index-Based Loops
Instead of using for-each or explicit iterators, loop through the array using its indices. This avoids any iterator conflicts entirely.
Bad (causes the error):
// Nested for-each loops on the same bullets array for (Bullet bullet : bullets) { bullet.update(delta); for (Bullet otherBullet : bullets) { // Check collision or other logic } }
Good (safe index-based loop):
// Use index loops to avoid iterator nesting for (int i = 0; i < bullets.size; i++) { Bullet bullet = bullets.get(i); bullet.update(delta); // Loop through other bullets with a separate index for (int j = 0; j < bullets.size; j++) { if (i != j) { // Skip the current bullet to avoid self-checks Bullet otherBullet = bullets.get(j); // Run your collision/other logic here } } }
2. Safely Modify the Array While Iterating
If you're trying to remove bullets (e.g., when they go off-screen), using an iterator directly (without nesting) works—but only if you use the iterator's own remove() method. Alternatively, use a reverse index loop to avoid index shifting issues.
Option A: Use Iterator.remove()
Iterator<Bullet> bulletIterator = bullets.iterator(); while (bulletIterator.hasNext()) { Bullet bullet = bulletIterator.next(); bullet.update(delta); if (bullet.isOffScreen()) { // Use the iterator's remove method—NOT bullets.remove(bullet) bulletIterator.remove(); } }
Option B: Reverse Index Loop
This is great if you need to remove multiple elements without worrying about index shifting:
// Loop from the end to the start for (int i = bullets.size - 1; i >= 0; i--) { Bullet bullet = bullets.get(i); bullet.update(delta); if (bullet.isOffScreen()) { bullets.removeIndex(i); } }
3. Iterate Over a Copy of the Array
If you absolutely need to use nested iteration (though index loops are better), create a copy of the array first and iterate over the copy. This way, the original array's iterator isn't conflicted:
// Create a copy of the bullets array Array<Bullet> bulletsCopy = new Array<>(bullets); for (Bullet bullet : bullets) { bullet.update(delta); // Iterate over the copy instead of the original for (Bullet otherBullet : bulletsCopy) { // Your logic here } }
Final Notes
Double-check line 53 in PlayState.java—that's where your problematic iteration is happening. Swap out the nested iterators or unsafe array modifications with one of the methods above, and your error should disappear.
内容的提问来源于stack exchange,提问作者Ali Almhmad

