# Can't find a mistake or removeAll() doesn't work

**URL:** <https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368>\
**Category:** Coding Questions\
**Created:** [March 3, 2020, 10:18pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368 "2020-03-03T22:18:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [March 3, 2020, 10:18pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/1 "2020-03-03T22:18:37Z")

</div>

Hello,

Please check the sketch at [https://www.openprocessing.org/sketch/851540](https://www.openprocessing.org/sketch/851540)

it seems that when run in [openprocessing.org](http://openprocessing.org) it passes but a few iterations of the `while()` loop of the `getRandom()` method, but it prints that the `size()` of `ArrayList candidates` decreases.

when run from the processing ide it shows that `removeAll()` method doesn’t work, the `size()` remains the same.

Please help, I don’t know what to do…

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [March 5, 2020, 5:36pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/2 "2020-03-05T17:36:02Z")

</div>

The wording of your post is not clear. You express some things don’t work and you need help, in a nutshell.

Let’s take a step back. Please explain what your code suppose to do, what it does right now (unintended behavior) and where are the culprit lines. Any theories?

On a side note, I tried running your sketch in the site and it seems to freeze my browser. Maybe if I wait long enough I could see what your code does. However, I am not sure what I suppose to see. I prefer if you explain it instead of us “The community” try to infer what your code does.

Kf

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [March 5, 2020, 6:09pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/3 "2020-03-05T18:09:42Z")

</div>

Ok. My bad, sorry.

What it’s supposed to do:  
Generate a list of random points on the screen at a minimal distance from each other but as tight as possible.  
The algorithm is as follows:  
Make a list of all possible places where a point can be placed, name the list candidates.  
While the candidates list is not empty, take a random point from there, add it to the output list, and remove that point and all candidates that are within its minimal distance neighborhood from candidates.

My idea was to check if it would be slow or not. It turns out not only to work too slow but not working at all…  
Well, it partly does, but my browser seems to terminate that long loop after a few iterations. Yours seems to run the loop, but there’s much work each iteration. Maybe it could run if the map size reduced and the min radius increased.

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [March 6, 2020, 12:10pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/4 "2020-03-06T12:10:33Z")

</div>

mmm I don’t get exactly what you want, but using ArrayList, if you want to remove all the list, I use `mylist.clear` , and if you want to remove one from the list I use `mylist.remove(posOfTheElementIWantToRemove)`

😅  
Hope this helps you in something

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [March 6, 2020, 6:56pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/5 "2020-03-06T18:56:20Z")

</div>

`removeAll()` method removes a collection from another collection. see [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#removeAll-java.util.Collection-](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#removeAll-java.util.Collection-)  
not `clear()`, not `remove()`, but something in between 🙂  
thanks for your interest!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 9, 2020, 4:31am UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/6 "2020-03-09T04:31:18Z")

</div>

Quick glance and first guess:

Why is `getNeighbors()` creating an ArrayList of **new** Loc objects? When in `getRandom()` you use `removeAll(), it should check for the _same_ elements to remove, shouldn’t it? But these aren’t the same elements – they are new elements that you just created. Instead, you get should actually get existing elements. Then removeAll would work.

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [March 11, 2020, 4:30am UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/7 "2020-03-11T04:30:46Z")

</div>

I see… So it compares by reference, not by value. I guess I’ll have to rewrite it into `ArrayList<Int>` instead of `ArrayList<Loc>`.  
Thank you, @jeremydouglass  
But I wonder how it happens to work in the browser…

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [March 11, 2020, 10:16am UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/8 "2020-03-11T10:16:19Z")

</div>

> [@LuckSmith](#):
>
> So it compares by reference, not by value.

Not exactly, but the default equals and hashCode methods will. Have you overridden them? See [https://www.baeldung.com/java-equals-hashcode-contracts](https://www.baeldung.com/java-equals-hashcode-contracts)

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [March 11, 2020, 7:32pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/9 "2020-03-11T19:32:06Z")

</div>

I have implemented `equals()` but not `hashCode()`. The article you gave says both are essential.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [March 12, 2020, 2:06pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/10 "2020-03-12T14:06:47Z")

</div>

Yes, equal objects should have equal hashcodes. Although that’s only important for certain types of collection. You might get away with just equals. I would also double check your equals method is returning true in all cases you think it should.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 12, 2020, 3:43pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/11 "2020-03-12T15:43:49Z")

</div>

Just a suggestion – it might be easier to make getNeighbors actually get the neighbors. Then removeAll would remove them correctly.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 6, 2020, 10:46pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/12 "2020-05-06T22:46:32Z")

</div>

5 posts were split to a new topic: [removeAll in p5.js (vs Processing)](https://discourse.processing.org/t/removeall-in-p5-js-vs-processing/20566)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 6, 2020, 10:48pm UTC](https://discourse.processing.org/t/cant-find-a-mistake-or-removeall-doesnt-work/18368/17 "2020-05-06T22:48:07Z")

</div>

5 posts were merged into an existing topic: [removeAll in p5.js (vs Processing)](https://discourse.processing.org/t/removeall-in-p5-js-vs-processing/20566/6)
