1

So I have looked a lot online on how to do this and what I get is this

ArrayList<ArrayList<Type>> arrayLists = new ArrayList<ArrayList<Type>>();
ArrayList<TypeA> typeA = new ArrayList<TypeA>();
ArrayList<TypeB> typeB = new ArrayList<TypeB>();
...

Assuming that I have a class called TypeA, and a class called TypeB, and the array list typeA would contain multiple objects of the class TypeA. The same goes for typeB

Now my question what do I put where Type is for the arrayList, and how do I add an arrayList to arrayLists? If I did this I would get an error:

arrayLists.add(typeA);

I am trying to make a list that contains arrayLists, if my approach is wrong please tell me.

1
  • 2
    I think you should mention whether typeA and typeB are related or not I guess Commented Jan 11, 2015 at 5:27

1 Answer 1

4

If you want an ArrayList which contains ArrayLists with unknown contents, use the type ArrayList<ArrayList<?>> for the outer list.

This code should compile with no problems:

ArrayList<ArrayList<?>> arrayLists = new ArrayList<ArrayList<?>>();
ArrayList<TypeA> typeA = new ArrayList<TypeA>();
ArrayList<TypeB> typeB = new ArrayList<TypeB>();

arrayLists.add(typeA);
arrayLists.add(typeB);
Sign up to request clarification or add additional context in comments.

2 Comments

I did not ask this question but can you explain a little more so I can vote u up?
? is a wildcard, not much explaining needed :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.