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?
GArlington - 05 Sep 2007 12:29 GMT
> Hello
>
[quoted text clipped - 26 lines]
> 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...
phi - 05 Sep 2007 13:05 GMT
GArlington schrieb:
>> Hello
>>
[quoted text clipped - 29 lines]
> 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...
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
see http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
GArlington - 05 Sep 2007 15:10 GMT
> GArlington schrieb:
>
[quoted text clipped - 37 lines]
> 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.
Piotr Kobzda - 05 Sep 2007 14:13 GMT
> session.setAttribute("note ", note);
> session.setAttribute("note", n);
> Any idea what is the differnce between the for(T t: c)-loop and the
> Iterator-methods?
A "note " not equals a "note".
piotr
Mihai Cazac - 05 Sep 2007 14:14 GMT
null
> in the sesson.
>
[quoted text clipped - 19 lines]
> 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.
Lew - 05 Sep 2007 14:26 GMT
> Hello
>
[quoted text clipped - 26 lines]
> 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".)

Signature
Lew
Lew - 05 Sep 2007 14:28 GMT
> 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.

Signature
Lew
phi - 05 Sep 2007 15:42 GMT
phi schrieb:
> Hello
>
[quoted text clipped - 26 lines]
> 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