0

How to make “array of ArrayList” that contains ArrayList<Integer>? I tried this

ArrayList<Integer>[] locOfDotCom = new ArrayList[3];

there is no error, but it didn't work. So how can I do it? I have tried other answers on SO but it is very confusing

1
  • 1
    there is no error but look at the warning Commented May 2, 2014 at 9:41

3 Answers 3

2

Once you've done that, you'll just have an empty array. You'll need to loop through and create an ArrayList<String> in each slot afterwards.

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

Comments

0

How to make “array of ArrayList” that contains ArrayList<Integer>?

Simply use List in place of static array.

List<List<Integer>> locOfDotCom = new ArrayList<List<Integer>>();

Always assign to interface type.

Comments

0

Use a LinkedList<ArrayList<Integer>>. The LinkedList makes sure that the ArrayList objects are always sorted in the same order, even if you add a new ArrayList.

6 Comments

What do you think about ArrayList? does it maintain the insertion order?
What is you point? Yes it does. Have I said something wrong? EDIT: All lists maintain the insertion order. They are lists.
why you used LinkedList?
Because it brings benefits (over an ArrayList) when you insert new elements or remove elements. When you know the position of those elements.
but in your post you talked about "always sorted in the same order". Look at the OP question again. OP is not asking about which one to in between ArrayList and LinkedList.
|

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.