0

I'm doing a problem on leetcode: given a list of integers, return a list of lists that contains all the possible permutation of these numbers. The class and method are the following:

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> permu = new ArrayList<ArrayList<Integer>>();
        more code here ...
        }
    }

The compiler threw an error "ArrayList<ArrayList<Integer>> cannot be converted to List<List<Integer>>". I know that the return type must be List<List<Integer>> instead of ArrayList<ArrayList<Integer>>, but I can't declare List<List<Integer>> permu = new List<List<Integer>>(); because list is an interface. How should I declare permu correctly, if I'm not allowed to change the return type in the permute method?

2 Answers2

1

You can create it like this:

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

Or even

List<List<Integer>> permu = new ArrayList<>();
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
0
List<List<Integer>> permu = new ArrayList<>();

This should work since it can implicitly understand what types should be there.

Asis
  • 319
  • 1
  • 2
  • 14