-2

Basically I have the following function where I need to convert ArrayList to List

public List<List<Integer>> somefunc(int[] nums) {
    ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();

    // logic

    return a;
}

How can I convert this ArrayList<ArrayList> to List<List>?

1
  • ArrayList is already a List... Commented Apr 3, 2019 at 12:16

3 Answers 3

5

You should always prefer programming to interfaces.

Change

ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();

to

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

Then you can add ArrayList<Integer> instances to a.

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

Comments

4

Instead of this:

ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();

Write this:

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

Or even this:

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

Comments

1

There is no need to "convert" it.

Just declare your a as

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

You can add any List<Integer> to your a:

a.add(new ArrayList<Integer>());

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.