Hello,
Please check the sketch at https://www.openprocessing.org/sketch/851540
it seems that when run in 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…
1 Like
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
1 Like
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.
1 Like
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
1 Like
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-
not clear()
, not remove()
, but something in between
thanks for your interest!
2 Likes
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.
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…
Not exactly, but the default equals and hashCode methods will. Have you overridden them? See https://www.baeldung.com/java-equals-hashcode-contracts
1 Like
I have implemented equals()
but not hashCode()
. The article you gave says both are essential.
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.
Just a suggestion – it might be easier to make getNeighbors actually get the neighbors. Then removeAll would remove them correctly.
5 posts were split to a new topic: removeAll in p5.js (vs Processing)
5 posts were merged into an existing topic: removeAll in p5.js (vs Processing)