In the following loop marker is a list which is is iterated through and each item is drawn on the canvas. However as the loop progresses I believe that the previous references are destroyed. How can I create a unique instance for each run of the loop?
//Draw AR markers in reverse order since the last drawn should be the closest
ListIterator<Marker> iter = collection.listIterator(collection.size());
while (iter.hasPrevious()) {
Marker marker = iter.previous();
marker.draw(canvas);
}
I’ve tried the following but to no avail. The loop reaches the second item then has a NoSuchElementException.
while (iter.hasPrevious()) {
int i = 0;
try {
Marker marker = iter.previous();
marker.draw(canvas);
++i;
System.out.println("Item number 1 " + i);
Marker marker1 = iter.next();
marker1.draw(canvas);
++i;
... repeated up to marker7 then catching the NoSuchElementException.
That is not the issue.
You are not creating new
Markerobjects, just reading it from an iterableCollection. The original collection (and probably other objects) contains references to the objects your are looping through, so they are not destroyed.Your second code only checks for
iter.hasPrevious()at the beginning of the loop, but it is doing many operations. If there are not enough elements in theCollection/iterator, then you get that exception when trying to reach for an element that does not exist (for example, doingiter.previous()afteriterhas reached the first element).