Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing elements from an arraylist

 
Mel Calvin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
ok, sourceList is the id of what?

regards.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic