0

In my servlet I have the following. All four are ArrayList<>, each with 5 values.

    request.setAttribute("interestEarnList", interestEarnList);
    request.setAttribute("numYear", numYear);
    request.setAttribute("endBalanceList", endBalanceList);
    request.setAttribute("startBalanceList", startBalanceList);

I want to display them on jsp file in the form of:

Year Number: ${numYear}, 
Beginning Balance of this year is: ${startBalanceList},
Ending Balance of this year is: ${endBalanceList},
Total interest earned this year: ${interestEarnList},

And loop this based on the size of numYear arrayList.

I have tried forEach but unfortnately it will display all numYear values, then all startBalanceList values, and so on.

FeCH
  • 133
  • 3
  • 15
  • 1
    Both of these talk about what you want to do but they only use two lists so you need to expand that to four for your case: http://stackoverflow.com/questions/31963297/how-to-zip-two-arraylists http://stackoverflow.com/questions/3833814/java-how-to-write-a-zip-function-what-should-be-the-return-type – Jerry Jeremiah Mar 08 '17 at 04:51
  • 1
    What about http://stackoverflow.com/questions/2118932/is-there-an-accepted-java-equivalent-to-pythons-zip or http://stackoverflow.com/questions/1365793/how-to-most-elegantly-iterate-through-parallel-collections – Jerry Jeremiah Mar 08 '17 at 04:53

1 Answers1

2

Here is a demonstration.

<%@ page import="java.util.*" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
    request.setAttribute("interestEarnList", Arrays.asList(11,22,33,44,55));
    request.setAttribute("numYear", Arrays.asList(1,2,3,4,5));
    request.setAttribute("endBalanceList", Arrays.asList(122,244,366,488,610));
    request.setAttribute("startBalanceList", Arrays.asList(111,222,333,444,555));
%>
    <c:forEach var="year" items="${numYear}" varStatus="status">
        Year Number: ${year}, <br/>
        Beginning Balance of this year is: ${startBalanceList[status.index]},<br/>
        Ending Balance of this year is: ${endBalanceList[status.index]},<br/>
        Total interest earned this year: ${interestEarnList[status.index]}<br/>
    </c:forEach>

With that dummy data, the output is

Year Number: 1, 
Beginning Balance of this year is: 111,
Ending Balance of this year is: 122,
Total interest earned this year: 11
Year Number: 2, 
Beginning Balance of this year is: 222,
Ending Balance of this year is: 244,
Total interest earned this year: 22
Year Number: 3, 
Beginning Balance of this year is: 333,
Ending Balance of this year is: 366,
Total interest earned this year: 33
Year Number: 4, 
Beginning Balance of this year is: 444,
Ending Balance of this year is: 488,
Total interest earned this year: 44
Year Number: 5, 
Beginning Balance of this year is: 555,
Ending Balance of this year is: 610,
Total interest earned this year: 55
rickz
  • 4,324
  • 2
  • 19
  • 30
  • I found a way to do this by creating a DTO to store all the information then store them inside an ArrayList. But your way is awesome and easy to understand. Thanks for the answer. – FeCH Mar 08 '17 at 14:44