• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing elements from an arraylist

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

I have a LinkedHashMap that contains ArrayLists. Something like this:




How can I iterate over the elements in the arrays in the map in a JSP page?
Do I need to use the nested or logic tag? Can anyone show me an example of how to print out e.g. "list1 row1"?

Thanks.
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<%

ArrayList list = new ArrayList();
ArrayList list2 = new ArrayList();
LinkedHashMap<Integer, ArrayList> map = new LinkedHashMap<Integer, ArrayList>();
list.add(0, "list1 row1");
list.add(1, "list1 row2");
list.add(2, "list1 row3");
list2.add(0, "list2 row1");
list2.add(1, "list2 row2");
list2.add(2, "list2 row3");
map.put(0, list);
map.put(1, list2);
request.getSession().setAttribute("Map",map);
%>
<logic:iterate id="map1" name="Map" indexId="index" >
<table>
<tr>
<logic:iterate id="list1" name="map1" property="value">
<table><tr><td><bean:write name="list1"/></td></tr></table>

</logic:iterate>
</tr>
</table>
</logic:iterate>
 
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
Yes that is working, thanks. However that will print both arrays, I found out that to print only one of them can be done with:
 
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
A problem with this code is that it will print out the whole list. How would you retrieve/print out e.g. the first elements of all arraylists in one iteration and then the second elements etc?
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i got the same problem today .
try this if you can use jstl:

<logic:iterate id="sourceList" name="${returnList[0]}">
 
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
ok, sourceList is the id of what?

regards.
 
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:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic