I’m getting an error when trying to create the linked list that says:
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The type LinkedList is not generic; it cannot be parameterized with arguments <String>
at LinkedList.main(LinkedList.java:7)
Anyone know how to fix this error? Here is the program:
import java.util.*;
public class LinkedList {
public static void main(String[] args) {
List<String> list = new LinkedList<String>();
Scanner input = new Scanner(System.in);
System.out.print("How many elements do you want to add: ");
int num = input.nextInt();
for(int i = 0; i < num; i++) {
System.out.print("Add Element: ");
String element = input.next();
list.add(element);
}
System.out.println();
System.out.println("LinkedList elements are: ");
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
Change
to
The source of the problem is that
LinkedListrefers to the class containing the codeclass LinkedList, notjava.util.LinkedList.Unqualified class names like
LinkedList(in contrast to “fully-qualified names” likejava.util.LinkedList) are resolved by looking for a match in a certain order. RoughlySection “6.5 Determining the Meaning of a Name” of the Java language specification explains in more detail.