# Help with a short game code

**URL:** https://discourse.processing.org/t/help-with-a-short-game-code/11108
**Category:** Coding Questions
**Created:** [May 10, 2019, 2:54pm UTC](https://discourse.processing.org/t/help-with-a-short-game-code/11108 "2019-05-10T14:54:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![majR](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@majR](https://discourse.processing.org/u/majR)
#### Post date: [May 10, 2019, 2:54pm UTC](https://discourse.processing.org/t/help-with-a-short-game-code/11108/1 "2019-05-10T14:54:23Z")

</div>

when i try to remove a part of a arraylist it says the error “ConcurrentModificationException”

here is the file : [https://drive.google.com/open?id=1-NATnwPv0Qw3abEjZzP8rBFMXgwngVrL](https://drive.google.com/open?id=1-NATnwPv0Qw3abEjZzP8rBFMXgwngVrL)

here is what gives the error

```auto
for(Laser actualLaser: laserList){
  println(actualLaser);
	for(int i = 0; i<4; i++){
		if(actualLaser.x > escudoArray[i].x-50 &&
			 actualLaser.x < escudoArray[i].x+50 &&
			 actualLaser.y > escudoArray[i].y-5 &&
			 actualLaser.y < escudoArray[i].y+5 &&
			 actualLaser.vel > 0){
			escudoArray[i].y = -500;
			laserList.remove(actualLaser);
		}
	}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 10, 2019, 3:56pm UTC](https://discourse.processing.org/t/help-with-a-short-game-code/11108/2 "2019-05-10T15:56:32Z")

</div>

We can’t use method **remove()** when using an “enhanced” `for ( : ) {}` style! 🚫

Use a vanilla `for ( ; ; ) {}` style instead. Preferably, w/ backwards iteration: 🔙

> [@\[SOLVED\] Py.processing bullet and enemy collision](https://discourse.processing.org/t/solved-py-processing-bullet-and-enemy-collision/8327/4):
>
> @solub, your solution is buggy! bug When we delete an item from a list, all elements w/ a higher index than the deleted item got their indices subtracted by 1! face_with_monocle They’re left-shifted, and therefore, the next iteration skips an item; which now got the same index as the just deleted item. arrow_left You can see that bug in this sample loop below: eye : nums = list(range(10)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for n in nums: print n nums.remove(n) print nums # [1, …

---

<div class="post-metadata">

### Author: ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)
#### Post date: [May 10, 2019, 4:12pm UTC](https://discourse.processing.org/t/help-with-a-short-game-code/11108/3 "2019-05-10T16:12:18Z")

</div>

couldn’t you use an iterator as well? something like

```auto
Iterator<String> it = myList.iterator();
while (it.hasNext()) { 
  if ("SOMESTRING".equals(it.next())) { 
    it.remove();
  }
}

```

might be slower on larger lists and i gotta say a for loop running n-0 is my preferred way too but the more options the better…right 😕

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 10, 2019, 4:17pm UTC](https://discourse.processing.org/t/help-with-a-short-game-code/11108/4 "2019-05-10T16:17:05Z")

</div>

> [@hotfooted](#):
>
> Couldn’t you use an Iterator as well?

The reason I prefer not to rely on an Iterator is b/c it’s incompatible w/ Pjs, in case I decide to deploy my sketch online as well. 🕸

---

<div class="post-metadata">

### Author: ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)
#### Post date: [May 10, 2019, 4:21pm UTC](https://discourse.processing.org/t/help-with-a-short-game-code/11108/5 "2019-05-10T16:21:02Z")

</div>

well that makes sense.
