Iterator help

Iterator<Socket> it = L.iterator();

while (it.hasNext()) {
Socket ns = it.next();

System.out.println("Neighbour: " + ns.getInetAddress());
}

How can I check the size of the iterator because I want to do something like

if (it.size == 0) System.out.println("no neighbours");
[349 byte] By [pouncer] at [2007-11-20 11:53:10]
# 1 Re: Iterator help
Iterators don't have a determinate size, they just keep going until there are no more items.

You don't need to know the size of the iterator, just whether it is empty or not. You can tell whether an iterator is empty with:if (!iterator.hasNext()) {
System.out.println("Iterator is empty");
}A less elegant alternative is to set a boolean false before the loop, and set it true inside the loop. If it is false after the loop is finished, there were no items.

Less is more...
M. van der Rohe
dlorde at 2007-11-10 2:14:00 >
# 2 Re: Iterator help
Perfect, thank you dlorde.
pouncer at 2007-11-10 2:14:57 >