Discussion Locked

Modify a list while iterating

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
The Dec. 2, Java TechMail discusses the capabilities of the ListIterator class. Have you used the ListIterator class to modify a list during iteration? Tell us about your experience.

1 reply

by tconnor

The example of setting all elements of a list to integer zero, as written, sets all elements to refer to the one Integer object "zero", and thus the list elements are not independent.

To set the list to truly independent integers, initially zero-ed, do this:

ListIterator listItr = list.listIterator();
while(listItr.hasNext()) {
Object obj = listItr.next();
listItr.set(new Integer(0));
}

This discussion is closed to new replies.