This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes JSP and the fly likes forEach in Java class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » JSP
Reply Bookmark "forEach in Java class" Watch "forEach in Java class" New topic
Author

forEach in Java class

Dan Parsons
Ranch Hand

Joined: May 30, 2007
Posts: 70
I have a JSTL forEach example that I would like to implement in a Java class instead of JSP.
Example:


Is the below attempt the right way?




David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Does it work? Seems like that'd be the absolute quickest way of finding out, considering you've already written a first attempt.

It's not entirely clear what you're trying to do, though; right now it looks like you're setting each movie to a string, then doing nothing with it.

Note that if you're using Java 1.5+ you may want to consider using the loop syntax introduced there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56528
    
  14

There's no need to use an iterator when looping over a List. For example iterating over a List<Item> :




[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

Dan,

No .. it doesn't look correct ... a couple questions ...

1. What does MovieListing.getList() return? a java.util.ArrayList of Strings, a java.util.ArrayList of MovieBeans or something else?
2. It looks like your JSP code places an ArrayList of MovieBean into scope and the toString() method of MovieBean prints what you want when you iterate using forEach. Is this what you want?
3. What do you want the Java code to do?


kf
SCJP 5.0 (preparing for SCWCD)
Dan Parsons
Ranch Hand

Joined: May 30, 2007
Posts: 70
Keith Flo wrote:Dan,

No .. it doesn't look correct ... a couple questions ...

1. What does MovieListing.getList() return? a java.util.ArrayList of Strings, a java.util.ArrayList of MovieBeans or something else?
2. It looks like your JSP code places an ArrayList of MovieBean into scope and the toString() method of MovieBean prints what you want when you iterate using forEach. Is this what you want?
3. What do you want the Java code to do?


Its not working and seems to print all the records per array.
Its suppose to print a Java java.util.ArrayList of MovieBeans.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56528
    
  14

You have not posted your updated code since my post. What's the status of things now?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: forEach in Java class
 
Similar Threads
Jstl error
howto display data from database into html
JSTL printing issue
Answer this if you know EL
Doubt in Attribute with JSTL.Thanks!