PDA

Click to See Complete Forum and Search --> : Iterator Problem


x-ice
Dec 31st, 2005, 05:09 PM
I am iterating through a collection of customer objects in an array list.
public void getCustomer() //TEST, DELETE AFTERWARDS
{
Iterator itCount = custDB.iterator();
Customer thisCustomer = (Customer)itCount.next();
//Loop tthrough all customers
while(itCount.hasNext())
{
thisCustomer.print(); System.out.println(); //Blank line for presentation
//Move to next customer
thisCustomer = (Customer)itCount.next();
}
}This code doesn't print the last object in the arraylist but it does print the first, and the rest. What could be the problem?

ComputerJy
Dec 31st, 2005, 06:09 PM
It's simple.
Because the last item doesn't (hasNext)

CornedBee
Dec 31st, 2005, 09:58 PM
The answer is cryptic AND insulting. ***?

Seriously, though, an iterator loop is written like thus:
Iterator it = getSomeIterator();
while(it.hasNext()) {
ActualType obj = (ActualType)it.next();
// do something
}
Or in Java5:
for(ActualType obj : iterable) {
// do something
}

x-ice
Jan 1st, 2006, 06:27 AM
Solutionpublic void getCustomer() //TEST, DELETE AFTERWARDS
{
Iterator itCount = custDB.iterator();
//Loop through all customers
do
{
//Move to next customer
Customer thisCustomer = (Customer)itCount.next();
thisCustomer.print(); System.out.println(); //Blank line for presentation
}while(itCount.hasNext());
}

CornedBee
Jan 1st, 2006, 07:35 AM
What, pray tell, is wrong with my loop? Unlike yours, it won't throw a NoSuchElementException if there are no elements in the collection.

ComputerJy
Jan 1st, 2006, 08:53 AM
No CornedBee, his is better.
'cause yours doesn't handle the first element

CornedBee
Jan 1st, 2006, 11:16 AM
Nonsense! You might want to confirm such claims first.
(Ignore the generics in the code; they do not change behaviour.)

import java.util.*;

public class IterTest
{
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add("1");
al.add("2");
al.add("3");

Iterator<String> it = al.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
Prints:
1
2
3

Which is the same as x-ice's method:

import java.util.*;

public class IterTest2
{
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add("1");
al.add("2");
al.add("3");

Iterator<String> it = al.iterator();
do {
System.out.println(it.next());
} while(it.hasNext());
}
}

However, remove the al.add lines, and the story's different. Output of my code:

That is, nothing, because printing all the elements of an empty list obviously yields nothing.

x-ice's code:
Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:427)
at IterTest2.main(IterTest2.java:15)


The semantics of Iterator state that it is only safe to call next() when a previous call to hasNext() returned true. x-ice's code calls next() before hasNext(), which is a bug.

It should be noted that these two snippets are semantically identical, except for the scope of it:
Iterator it = coll.iterator();
while(it.hasNext()) {
it.next();
}
for(Iterator it = coll.iterator(); it.hasNext(); ) {
it.next();
}

An lo and behold, here's what each and every Enumeration or Iteration loop looks like in the Java API docs. The code example from java.util.Enumeration:
for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {
System.out.println(e.nextElement());
}
(Iterator and Enumeration have identical semantics. The two differences are the changed method names and the added remove() method of Iterator.)

And here, from the presentation of the new for-loop in Java 5, are the examples of old-style loops with generics:
void cancelAll(Collection<TimerTask> c) {
for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); )
i.next().cancel();
}
for (Iterator i = suits.iterator(); i.hasNext(); ) {
Suit suit = (Suit) i.next();
for (Iterator j = ranks.iterator(); j.hasNext(); )
sortedDeck.add(new Card(suit, j.next()));
}


Bottom line, as arrogantly as I can put it: I'm right, you're wrong. End of discussion.

ComputerJy
Jan 1st, 2006, 02:17 PM
(Wow)
I didn't really read the stuff you put in your message but you seem really pissed in the last line.
http://vbforums.com/attachment.php?attachmentid=37788

x-ice
Jan 1st, 2006, 04:22 PM
My code works fine, no exceptions are thrown. I have tested it.

CornedBee
Jan 1st, 2006, 04:27 PM
Have you tested it with an empty customer database?

x-ice
Jan 3rd, 2006, 07:02 PM
Have you tested it with an empty customer database?I am populating an Array List (represents a customer database) earlier in the program. In order to deal with this possible exception: if(custDB.Size() == 0)
{
//Deal with this
}Also for this coursework assignment the database will never do empty (as this university module doesn't cover exception handling). We have been told to assume that.

p.s. Take it easy, nobody said your code is wrong, better or worse.

System_Error
Jan 3rd, 2006, 07:55 PM
p.s. Take it easy, nobody said your code is wrong, better or worse.

I just now saw this thread. Anyone who has a little experience with Tiger would know CB is correct in all that he said. Programmers are touchy people, especially when someone who doesn't know what they're talking about jumps in and says you're wrong when it's obvious THEY are the one wrong. I would have done the same thing if faced with the situation. People like that just piss me off.

x-ice
Jan 4th, 2006, 06:45 AM
I didn't post this topic to start an arguement. If i have offended anyone i'm sorry.

System_Error
Jan 4th, 2006, 02:24 PM
I didn't post this topic to start an arguement. If i have offended anyone i'm sorry.

I wasn't implying you, but lets just drop it and hopefully some people learned a few things.