0

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.

2
  • 1
    records is an object, not a list. Why would you want to iterate over it? Or did you mean getRecords instead?
    – CoolBeans
    Sep 27, 2011 at 22:11
  • 1
    In your method ArrayList<String> getRecordsDetail(..) -- you should return List<String> not ArrayList<String> unless there's some reason that it's important for the caller to know that it's actually an ArrayList. (unrelated to your question) If it's not important to the caller, I'll even return Collection<OfSomething>
    – Stephen P
    Sep 27, 2011 at 22:22

2 Answers 2

1

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.

0
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.