• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forEach in Java class

 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no need to use an iterator when looping over a List. For example iterating over a List<Item> :



 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have not posted your updated code since my post. What's the status of things now?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic