Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
phi - 05 Sep 2007 09:19 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.