Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have created an object which contains all the results from a query ran against the database. these results are then stored in an Array.

I am trying to convert this array into an ArrayList so that it can be used to import into a document.

I have tried:

//trustSummary is the name of my array
ArrayList data = new ArrayList(Arrays.asList(trustSummary));

But this appears to be causing issues (ClassCastException) when I am trying to run the below code in another part of the application. Would anyone please be able to point in the correct direction to resolve this issue.

for (Iterator rows = data.iterator(); rows.hasNext();)
   ArrayList row = (ArrayList) rows.next();
share|improve this question
1  
data is the ArrayList. Not its elements. –  ogzd Feb 14 at 13:15
3  
what is the type of trustSummary ? –  ogzd Feb 14 at 13:18
 
I think you have here an ArrayList containing an array of objects (each array of objects is a row of the result). Try casting to Object[] in the for loop. –  Averroes Feb 14 at 13:19
add comment

3 Answers

The ArrayList(Collection) constructor you are using here in the part where you write new ArrayList(Arrays.asList(trustSummary)); creates an ArrayList with the elements from the source collection, it does not create an ArrayList with another collection as element.

So you need either to fix that line, or change the line

ArrayList row = (ArrayList) rows.next();

to

TrustSummary ts = (TrustSummary) rows.next();

Generally I would advise using generics, to avoid that sort of confusion regarding a collection's types.

In your case that would either be for example

List<TrustSummary> summaries = new ArrayList<TrustSummary>(Arrays.asList(trustSummary));
share|improve this answer
add comment

Your code would only work if trustSummary is:

ArrayList[] trustSummary;

or: (though this doesn't really make sense, because then the array would only have 1 element, thus you may as well work with the element directly)

ArrayList trustSummary;

Both of the above works because Arrays.asList takes either an array or a bunch of items you want in the array as parameters.

The reason trustSummary needs to be defined as above (with ArrayList being the elements) is because the iterator grabs the elements one at a time from data (which was converted from the array) (so the return type of next() is the type used in the array (ArrayList in this case)).

This would work:

String[] trustSummary = {"1", "2", "3"};
ArrayList<String> data = new ArrayList<String>(Arrays.asList(trustSummary));
for (Iterator rows = data.iterator(); rows.hasNext();)
  System.out.println((String)rows.next());

Or this:

String trustSummary = "1";
ArrayList<String> data = new ArrayList<String>(Arrays.asList(trustSummary));
for (Iterator rows = data.iterator(); rows.hasNext();)
  System.out.println((String)rows.next());

Replace String with the desired type and change appropriately.

share|improve this answer
add comment
for (Iterator rows = data.iterator(); rows.hasNext();) 
{
    Map.Entry entry = (Map.Entry) rows.next();
    ArrayList row = (ArrayList) entry.getValue(); 
}
share|improve this answer
5  
Where do you find Map in the question? –  Dukeling Feb 14 at 13:17
 
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. –  Jon Egerton Feb 14 at 13:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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