the new for loop for Collections does not perform the same as theIterator for Tomcat-sessions

P

phi

Hello

We tried the following code using JDK1.6 and tomcat 5.5 (and tomcat 6.0
as well) to enter a "note"-Object into a session.
The list-Variable is an ArrayList and therefore a Java Collection.

The new for-Loop does NOT do the job: the object "note" is always null
in the sesson.

for(Note note : list){
if(note.getNoteId() == noteId.intValue()){
session.setAttribute("note ", note);
break;
}
}

We converted to the old java style loop (using the iterator) and see:
it works!

Iterator iter = list.iterator();
while(iter.hasNext()){
Note n = (Note) iter.next();
if(n.getNoteId() == noteId.intValue()){
session.setAttribute("note", n);
break;
}
}

Any idea what is the differnce between the for(T t: c)-loop and the
Iterator-methods?
 
G

GArlington

Hello

We tried the following code using JDK1.6 and tomcat 5.5 (and tomcat 6.0
as well) to enter a "note"-Object into a session.
The list-Variable is an ArrayList and therefore a Java Collection.

The new for-Loop does NOT do the job: the object "note" is always null
in the sesson.

for(Note note : list){
if(note.getNoteId() == noteId.intValue()){
session.setAttribute("note ", note);
break;
}
}

We converted to the old java style loop (using the iterator) and see:
it works!

Iterator iter = list.iterator();
while(iter.hasNext()){
Note n = (Note) iter.next();
if(n.getNoteId() == noteId.intValue()){
session.setAttribute("note", n);
break;
}
}

Any idea what is the differnce between the for(T t: c)-loop and the
Iterator-methods?

Is there for(T t: c) loop? Where did you find this syntax and what
does it do? I am familiar with for(init; cond; incr;) loop...
 
M

Mihai Cazac

null
in the sesson.

for(Note note : list){
if(note.getNoteId() == noteId.intValue()){
session.setAttribute("note ", note);
break;
}
}

We converted to the old java style loop (using the iterator) and see:
it works!

Iterator iter = list.iterator();
while(iter.hasNext()){
Note n = (Note) iter.next();
if(n.getNoteId() == noteId.intValue()){
session.setAttribute("note", n);
break;
}
}

Any idea what is the differnce between the for(T t: c)-loop and the
Iterator-methods?


A newbie question: session.setAttribute("note ", note)
is the same thing as session.setAttribute("note", n)
I see there a blank in "note " in the first case.
 
L

Lew

phi said:
Hello

We tried the following code using JDK1.6 and tomcat 5.5 (and tomcat 6.0
as well) to enter a "note"-Object into a session.
The list-Variable is an ArrayList and therefore a Java Collection.

The new for-Loop does NOT do the job: the object "note" is always null
in the sesson.

for(Note note : list){
if(note.getNoteId() == noteId.intValue()){
session.setAttribute("note ", note);
break;
}
}

We converted to the old java style loop (using the iterator) and see:
it works!

Iterator iter = list.iterator();
while(iter.hasNext()){
Note n = (Note) iter.next();
if(n.getNoteId() == noteId.intValue()){
session.setAttribute("note", n);
break;
}
}

Any idea what is the differnce between the for(T t: c)-loop and the
Iterator-methods?

None.

The difference must be in the part of the code that you do not show us. Since
the syntax you say works has only raw types in it, I suspect your trouble
relates to generics abuse.

It is best to provide complete examples that illustrate your problem. (Google
for "SSCCE".)
 
L

Lew

Lew said:
It is best to provide complete examples that illustrate your problem.
(Google for "SSCCE".)

Or the other respondents who note your extra blank have the right of it.
 
G

GArlington

GArlington schrieb:





The for(<Type> <varibale> : <Collection>) - Loop is a new Construct
since JDK 1.5.
As I have mentioned, we are using JDK 1.6 (in Tomcat 6).
The for(<Type> <variable> : <Collection>) - Loop
seehttp://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html

Thanks, did not notice it before.
 
P

phi

phi said:
Hello

We tried the following code using JDK1.6 and tomcat 5.5 (and tomcat 6.0
as well) to enter a "note"-Object into a session.
The list-Variable is an ArrayList and therefore a Java Collection.

The new for-Loop does NOT do the job: the object "note" is always null
in the sesson.

for(Note note : list){
if(note.getNoteId() == noteId.intValue()){
session.setAttribute("note ", note);
break;
}
}

We converted to the old java style loop (using the iterator) and see:
it works!

Iterator iter = list.iterator();
while(iter.hasNext()){
Note n = (Note) iter.next();
if(n.getNoteId() == noteId.intValue()){
session.setAttribute("note", n);
break;
}
}

Any idea what is the differnce between the for(T t: c)-loop and the
Iterator-methods?

Thank you all! This bloody extra blank "note " != "note" was a typo.
Many thanks for debugging

phi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top