I have a list which has elements 1 through 10. I try to remove the prime numbers 2,3,5,7 from it and then print the rest of the list using iterator.But this code throws a NoSuchElementException. this is my code :
public static void editerate2(Collection<Integer> list3)
{
Iterator<Integer> it=list3.iterator();
while(it.hasNext())
{
if(it.next()==2 || it.next()==3 || it.next() ==5 || it.next()==7 )
{
it.remove();
}
}
System.out.println("List 3:");
System.out.println("After removing prime numbers : " + list3);
}
What's the correct way of doing this? Also what's the difference between using "|" and "||" ???
it.next()once per iteration.|and||, that's a topic for another question- although if you're particularly curious, a google search for "Bitwise Operator" may help you out.