I am trying to create a LinkedList of LinkedLists in Java.
The following code segment is giving an error. I am using java 11 and util.List
No idea why I am getting this error..
N = in.read();
List<List<Integer>> L;
L = new LinkedList<>();
for( i = 0;i<N;i++) L.add(new LinkedList<>());
It gives the following errors:
A.java:25: error: cannot infer type arguments for LinkedList
L = new LinkedList<>();
^
reason: cannot use '<>' with non-generic class LinkedList
A.java:26: error: cannot infer type arguments for LinkedList
for( i = 0;i<N;i++) L.add(new LinkedList<>());
^
reason: cannot use '<>' with non-generic class LinkedList
How should I go on resolving this?
Okay, so just to test I created a dummy class just to create LinkedList of LinkedLists. Here is the full program:
import java.util.*;
class Dummy
{
public static void main(String[] args)
{
List<List<Integer>> L;
L = new LinkedList<>();
for(int i = 0;i<10;i++) L.add(new LinkedList<>());
}
}
Again, these errors:
A.java:7: error: cannot infer type arguments for LinkedList
L = new LinkedList<>();
^
reason: cannot use '<>' with non-generic class LinkedList
A.java:8: error: cannot infer type arguments for LinkedList
for(int i = 0;i<10;i++) L.add(new LinkedList<>());
^
reason: cannot use '<>' with non-generic class LinkedList
Edit: Okay, works fine when I use import java.util.List and import java.util.linkedList instead of import java.util.*
As pointed out in the comments there is probably some issue with my build path
LinkedList, by any chance?LinkedListisn't generic is a big warning flag.java.util.LinkedList(andjava.util.List) explicitly, rather than using a wildcardimportstatement. Sincejava.util.LinkedListis a generic class, the compiler is definitely picking up some non-generic classLinkedListfrom somewhere.LinkedListsomewhere that the compiler is looking. It may only be a left-over.classfile. Try cleaning out your build artifacts.