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

doesnt show any error. My question is why are these showing errors ?

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

// Ignore this when answering, it's a duplicate question
// List<List<Integer>> a = new ArrayList<ArrayList<Integer>>();

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

Why do I not have to declare new inside the ArrayList declaration?

weston
  • 54,145
  • 21
  • 145
  • 203
asddf
  • 113
  • 5

5 Answers5

1

ArrayList<List<Integer>> is the type. It's an array list of lists of integers. When you create a new instance of a type by calling its constructor, you use the new keyword. That's why you get an error with the first and the third snippets -- you are trying to call additional constructors when you only need to call the constructor of the main type.

The second error is because the instantiation of the inner type is more specific/restrictive than the declared type, so it does not fulfill the contract and could break for callers that expect that contract to be fulfilled. You can still set the inner list to an ArrayList since it inherits from List, but you would do that when you instantiate the list.

ArrayList<List<Integer>> test = new ArrayList<List<Integer>>();
test.add(new ArrayList<Integer>());
pvg
  • 2,673
  • 4
  • 17
  • 31
S.C.
  • 1,152
  • 8
  • 13
  • You should probably expand a bit on why it's ok to specialize the type of the container but not the type of the contained objects. That's actually a nice and not-instantly-obvious question the poster raised. – pvg Feb 18 '17 at 22:35
0

Example 1

You declare generic types inside the angle brackets, not construct an instance of an object.

For this:

error: illegal start of type 
            List<List<Integer> >a =new ArrayList<new List<Integer>() >();
                                                 ^

This error shows that you cannot use the new keyword in a generic type.

Example 3

Your third example shows a similar error:

error: illegal start of type
            List<List<Integer> >a =new ArrayList<new ArrayList<Integer>() >();
                                                 ^

The generic type represents a class, not an instance.

Example 2

For the second example, the error message is telling you that the generic types must be the same. You cannot apply inheritance like this for generic types:

error: incompatible types: ArrayList<ArrayList<Integer>> cannot be converted to List<List<Integer>>
            List<List<Integer> >a =new ArrayList<ArrayList<Integer> >();
                                   ^

Using generic wildcards

To declare a generic list of List subtypes, you could use the wildcard expression ? extends List as the generic type:

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

For more on generics oracle has a good overview and the Java Tutorial has a nice guide.

Community
  • 1
  • 1
krock
  • 28,904
  • 13
  • 79
  • 85
0
List<List<Integer> >a =new ArrayList<new List<Integer>() >();

This is wrong because you do not need to put the parentheses inside the inner declaration because allocation of memory to the inner lists will happen later.

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

This is wrong because this line initializes only the outer List. So you can typecast the outer ArrayList into a list because List is its superclass. However, since you shouldn't initialize the inner list just yet, it should still remain a List.

You can consider it like this. Every element of the inner set of List can actually be any kind of class that extends a List and not necessarily an ArrayList. You need to initialize them individually when you need to use it.

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

This one is a combination of both the above errors.

Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
0

The angular brackets <> in java mean generics. What is expected inside the angular brackets is a type (class or interface), not object. When you use new operator in front of a type, it means an object of that type at that point. That's the reason you get an error when you use the new operator inside the angular brackets.

VHS
  • 9,534
  • 3
  • 19
  • 43
-2

List<Integer> and ArrayList<Integer> are classes.

new ArrayList<Integer>() is objects.

<> - it is not ArrayList declaration, there you specify type of elements of ArrayList.

Pavlo Plynko
  • 586
  • 9
  • 27