The Artificial Intelligence Forum                             data mining forum
This forum is to discuss artificial intelligence. You can ask anything about artificial intelligence : science-fiction, robots, consciousness, movies and so on! Also, you can discuss anything related to implementing artificial intelligence in machines such as programming, algorithms, etc. No registration is required!  
[JAVA] Removing elements from a list while iterating
Posted by: Johnston
Date: July 08, 2010 03:26AM

Some good code:

Iterator<Integer> iter = l.iterator();
while (iter.hasNext()) {
if (iter.next().intValue() == 5) iter.remove();
}


Another version:

for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) {
String s = iter.next();
if (s.equals("test" ) ) {
iter.remove();
}
}



Edited 1 time(s). Last edit at 07/23/2010 08:05PM by webmasterphilfv.

Options: ReplyQuote


This forum is powered by Phorum and provided by P. Fournier-Viger (© 2012).
Terms of use.