aspose file tools*
The moose likes Struts and the fly likes Accessing elements from an arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing elements from an arraylist" Watch "Accessing elements from an arraylist" New topic
Author

Accessing elements from an arraylist

Mel Calvin
Greenhorn

Joined: Sep 11, 2007
Posts: 9
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

Joined: Sep 12, 2007
Posts: 300
<%

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

Joined: Sep 11, 2007
Posts: 9
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

Joined: Sep 11, 2007
Posts: 9
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

Joined: Sep 12, 2007
Posts: 300
hey i got the same problem today .
try this if you can use jstl:

<logic:iterate id="sourceList" name="${returnList[0]}">
Mel Calvin
Greenhorn

Joined: Sep 11, 2007
Posts: 9
ok, sourceList is the id of what?

regards.
 
jQuery in Action, 2nd edition
 
subject: Accessing elements from an arraylist