• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

c:forEach

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code:

ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
ArrayList list3 = new ArrayList();
list1.add(0, "list1-row1");
list1.add(1, "list1-row2");
list1.add(2, "list1-row3");
list2.add(0, "list2-row1");
...
list3.add(2, "list3-row3"):

HashMap map = new HashMap();
map.put("List1", list1);
map.put("List2", list2);
map.put("List3", list3);
request.setAtttribute("MAP", map);
forwarding to the jsp-page...

If I in a jsp-page wanted to print out the first rows the three arrays, how could that be achieved? I.e. retrieve only one element per iteration per array. I just can't get this to work..

Regards,
Mel.
[ September 17, 2007: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we have three Lists bound to request scope with names list1, list2 and list3 and assuming that all three lists have same size we can do this.



If you really want to use HashMap and this logic to be generic then i would recommend you to write a tag and implement the logic in the tag class.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not clear what you are trying to achieve. What do you mean by "retrieve only one element per iteration per array"?

By nature, only one element will be retrieved per iteration so I'm not sure what's "special" about what you are trying to do?
 
Mel Calvin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a hashmap of arrays, if I iterate over the map and prints the elements, it will print the arrays as a whole. I want to print the elements of the arrays which is not that easy, however, I have come oup with a solution:


This code works..
 
it's a teeny, tiny, wafer thin ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic