4

I have this method

public List<List<Stat>> ConvertReportCases (ArrayList<ArrayList<Stat>> stats)

It is an interface implementation where the return type is a List<List<Stat>>. In general how do I convert from a nested ArrayList to List in Java?

Thanks

Edit Thanks for the suggestions I could make my parameter passed as List but then it would be wrong since the caller will have to pass it as List, whereas in my particular case I could have different versions (polymorphism) to check this issue.

5
  • 2
    An ArrayList already implements the List interface, i don't see your point there? Commented Jun 6, 2012 at 8:05
  • 2
    And I actually don't understand, why you have ArrayLists in the first place. You should always work with Lists (which are one of the implementations of the List interface, like ArrayList). So you can create your list like this: List<Stat> list = new ArrayList<Stat>(); Commented Jun 6, 2012 at 8:07
  • @MichaelLaffargue Perhaps he needs to pass it to a method that only accepts a List<List<Stat>>? Commented Jun 6, 2012 at 8:08
  • @brimborium ArrayList adds new methods. It makes sense to use an ArrayList if you want to use some of those methods. Commented Jun 6, 2012 at 8:17
  • @Pablo I see, very good point. I never used the different implementations for their additional methods, but just for their different implementation (and thus runtime performance/memory usage). Commented Jun 6, 2012 at 8:22

3 Answers 3

5

Really, there is no converting needed.

If you can't touch the declaration of the list, you should change the method signature to this. It uses wildcards.

public List<? extends List<Stat>> convertReportCases(List<? extends List<Stat>> stats);

However, the most clean way would be to change the method signature to

public List<List<Stat>> convertReportCases(List<List<Stat>> stats);

And declare the list as

List<List<Stat>> list = new ArrayList<List<Stat>>();
for (List<Stat> sublist : list) {
    sublist = new ArrayList<Stat>();
}

If you use List methods, you will be ok, there will be no converting needed. And since ArrayList is-a List and is practically only useful for its List methods, you should be ok!

Sign up to request clarification or add additional context in comments.

3 Comments

Notice the small letter in the beginning of convertReportCases(). It's Java's convention to name methods this way.
Is the concept of interfaces and implementing classes in Java clear to you, or should we provide some more resources?
Yup this should do, thanks very much for the suggestions. I learnt a new thing today !
0

You have to iterate through the fist list and convert each list. Something like:

public List<List<Stat>> ConvertReportCases (ArrayList<ArrayList<Stat>> stats) {
    List<List<Stat>> list = new ArrayList<List<Stat>>();
    for (List<Stat> l : stats) {
        list.add(l);
    }
    return list;
}

The problem appears because of generic covariance. You can find more here.

Comments

0
public List<List<Stat>> ConvertReportCases (ArrayList<ArrayList<Stat>> stats) {
   List<List<Stat>> newList = new ArrayList<List<Stat>>(stats.size());
   for (ArrayList<Stat> arrayList : stats) {
      newList.add(arrayList);
   }
   return newList;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.