Harry
Harry

Reputation: 4773

Use of Iterator in java

I have a object type list like

List<Object> getRecords = records.getAllRecords();
ArrayList<String> recordsDetail = new ArrayList<String>();
recordsDetail = this.getRecordsDetail(getRecords.get(3));
model.addAttribute("recordsDetail", getRecords.get(3));

But when I am writing the method like

ArrayList<String> getRecordsDetail(Object records) {

}

Now I don't know how I can Iterate over records as records has some values as vector.

Any help will be appreciated. Thanks

If I do the same thing in JSP using spring MVC its working fine like

<c:forEach var="sData" items="${recordsDetail}">
        <tr>
            <td>
                <table border="0">
                    <c:forEach var="sValues" items="${sData}" varStatus="colNo">
                        <c:choose>
                            <c:when test="${colNo.count> 5 and colNo.count<9}">
                                <tr>
                                    <td>${sValues}&nbsp;&nbsp;</td>
                                <tr>
                            </c:when>
                        </c:choose>
                    </c:forEach>
                </table>
                <hr>
            </td>
        </tr>
    </c:forEach>

But I want to do this processing in spring MVC controller and display the final results in JSP. So I am not able to find the way how i can iterate records in controller class.

Upvotes: 0

Views: 378

Answers (2)

DwB
DwB

Reputation: 38338

A few things.

First, don't initialize a local variable then set it immediately to something. Instead, just initialize to the desired value (note List type and not ArrayList type, that is a good practice or maybe just my preference).

List<String> recordsDetail = this.getRecordsDetail(getRecords.get(3));

Next, iterate over records

List<String> getRecordsDetail(Object records)
{
  if (records instanceof Vector)
  {
    Object blam;
    Iterator iterator;
    Vector recordVector = (Vector)records;

    iterator = recordVector.iterator();
    while (iterator.hasNext())
    {
      blam = iterator.next();
      // do something with blam.
    }
  }
}

I don't know the type of the records elements, so I used Object if you know the type, then use that instead of Object.

Upvotes: 1

Jim Garrison
Jim Garrison

Reputation: 86774

This should be

ArrayList<String> getRecordsDetail(List<Object> records) {

}

If you can be more specific about the type of object returned by records.getAllRecords() you should use List<the-object-type-returned-by-getAllRecords> instead.

Upvotes: 0

Related Questions