I have a function that returns a list of lists of integers
public List<List<Integer>> threeSum(int[] nums)
Obviously I can't directly instantiate a List so I chose to use an ArrayList and tried to instantiate my return value as such:
List<List<Integer>> ans = new ArrayList<ArrayList<Integer>>();
The above did not work but this did:
List<List<Integer>> ans = new ArrayList<List<Integer>>();
My understanding is that List
is an interface that ArrayList inherits from. Why, then, can I instantiate an ArrayList of Lists and be ok but I can't instantiate an ArrayList of ArrayLists?
For readability the first few lines of my function look as such:
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
if (nums.length < 3) return ans;