# Cannot find a class or type named “Iterator”

**URL:** <https://discourse.processing.org/t/cannot-find-a-class-or-type-named-iterator/36491>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [April 21, 2022, 8:18am UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-iterator/36491 "2022-04-21T08:18:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiajia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jiajia/32/16099_2.png) [@jiajia](https://discourse.processing.org/u/jiajia)\
**Post date:** [April 21, 2022, 8:18am UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-iterator/36491/1 "2022-04-21T08:18:40Z")

</div>

\<  
ArrayList particles;

void setup(){  
size(200,200);  
particles = new ArrayList();  
}  
void draw(){  
background(255);  
particles.add(new Particle(new PVector(width/2,50)));  
Iterator it = particles.iterator();  
while(it.hasNext()){  
Particle p = it.next();  
p.run();  
if(p.isDead()){  
it.remove();  
}  
}  
}  
class Particle{  
PVector location;  
PVector velocity;  
PVector acceleration;  
float lifespan;

Particle(PVector l){  
acceleration = new PVector(0,0.05);  
velocity = new PVector(random(-1,1),random(-2,0));  
location = l.copy();  
lifespan = 255.0;  
}

void run(){  
update();  
display();  
}

void update(){  
velocity.add(acceleration);  
location.add(velocity);  
lifespan -= 2.0;  
}

void display(){  
stroke(0,lifespan);  
fill(0,lifespan);  
ellipse(location.x,location.y,8,8);  
}

boolean isDead(){  
if(lifespan \< 0.0){  
return true;  
}else{  
return false;  
}  
}  
}

> 

 ![屏幕截图 2022-04-21 161443](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/f/1f38f54ae1e02bbbd728df466731d374e1b1d084.jpeg)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 21, 2022, 10:35am UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-iterator/36491/2 "2022-04-21T10:35:28Z")

</div>

Hello,

Please follow these instructions as a courtesy to this community:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

I have come across this example before and was able to correct it with some research.

Processing 4.0b7 shows this console message:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/3/33e1b7af8861fe1dfb073a7cab111dc6f4d43456.png)

After you add the _import_ you will get this error:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/b/4b89bc18da5d61763a4d31b70a41ad0b61485815.png)

A search in this forum for that _line 23_ (above picture) will find a topic that will give you some insight.

The steps I took worked to resolve this issue:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/5/e5804e2496d35c1b35a7c44637e845266fb4e72e.png)

`:)`

---

<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:** [April 21, 2022, 12:22pm UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-iterator/36491/3 "2022-04-21T12:22:37Z")

</div>

As @glv’s already pointed out, you need to import the Iterator interface before using it:  
`import java.util.Iterator;`

- [docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Iterator.html](http://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Iterator.html)

Alternatively you can use a regular `for ( ; ; )` loop, but making sure to decrease its iterator variable each time you invoke method **remove()**:

- [docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#remove(int)](http://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#remove(int))

```auto
for (int i = 0; i < particles.size(); ++i) {
  final Particle p = particles.get(i);
  p.run();
  if (p.isDead()) particles.remove(i--);
}

```

It’s even simpler if you iterate it backwards, so you don’t have to worry about decreasing the iterator for each **remove()**:

```auto
for (int i = particles.size(); i-- > 0; ) {
  final Particle p = particles.get(i);
  p.run();
  if (p.isDead()) particles.remove(i);
}

```

For a more complex but more efficient (b/c we completely avoid container’s element left-shifting) backwards technique, see this sketch’s **mousePressed()** code block implementation:

- “Clickable Balls”: [studio.ProcessingTogether.com/sp/pad/export/ro.PKnDDqu1vYo](http://studio.processingtogether.com/sp/pad/export/ro.PKnDDqu1vYo)

---

<div class="post-metadata">

**Author:** ![jiajia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jiajia/32/16099_2.png) [@jiajia](https://discourse.processing.org/u/jiajia)\
**Post date:** [April 21, 2022, 2:51pm UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-iterator/36491/5 "2022-04-21T14:51:29Z")

</div>

OH YEA !! Very thank you for your help 😶

---

<div class="post-metadata">

**Author:** ![jiajia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jiajia/32/16099_2.png) [@jiajia](https://discourse.processing.org/u/jiajia)\
**Post date:** [April 21, 2022, 2:54pm UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-iterator/36491/6 "2022-04-21T14:54:16Z")

</div>

I learned it with your help So THANKS！ 🙂
