You can remove from the collection (well, some collections) iterate over it using iterator.remove() - but you can't usually add to it.
However, as newacct notes in the comments, the ListIterator interface includes the add method, so you can change your code as follows:
public static void main(String[] args) { Collection<Integer> src = new ArrayList<Integer>(); List<Integer> dest = new ArrayList<Integer>(); src.add(2); src.add(7); src.add(3); src.add(2201); src.add(-21); dest.add(10); while (src.size() != 0) { for (ListIterator<Integer> li = dest.listIterator(); li.hasNext() ;) { int min = Collections.min(src); li.add(min); src.remove(min); } } }
Note that dest should now be declared as List, not Collection, and you need to explicitly expand the for loop. However, I'm still not sure why you repeat dest in the first place. You add an element at each iteration so that you never reach the end.
What happened to this?
while (src.size() != 0) { int min = Collections.min(src); dest.add(min); src.remove(min); }
Or, as others have said, just call sort() - go to your custom Comparator if you need to.
source share