• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

c:forEach

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Marshal
Posts: 67418
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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 wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic