Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"LinkedList is not generic" error Java

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));
        }
    }
}
like image 553
Bryan Avatar asked Mar 09 '12 18:03

Bryan


People also ask

Is LinkedList indexed in Java?

The elements are not indexed, so random access like an array is not possible. Instead, we traverse from the beginning of the list and access the elements. In Java, the LinkedList class is the doubly-linked list implementation of the List and Deque interfaces. The LinkedList class is present in the java.

How do you update a LinkedList in Java?

LinkedList. set() method is used to replace any particular element in the linked list created using the LinkedList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method.

Can LinkedList be implemented in Java?

Java provides a built LinkedList class that can be used to implement a linked list. In the above example, we have used the LinkedList class to implement the linked list in Java. Here, we have used methods provided by the class to add elements and access elements from the linked list.


3 Answers

Change

new LinkedList<String>()

to

new java.util.LinkedList<String>()

The source of the problem is that LinkedList refers to the class containing the code class LinkedList, not java.util.LinkedList.

Unqualified class names like LinkedList (in contrast to "fully-qualified names" like java.util.LinkedList) are resolved by looking for a match in a certain order. Roughly

  1. Look for a containing class.
  2. Look for a non-wildcard import.
  3. Look for a class in the same package.
  4. Look for a wildcard import.

Section "6.5 Determining the Meaning of a Name" of the Java language specification explains in more detail.

like image 163
Mike Samuel Avatar answered Oct 09 '22 23:10

Mike Samuel


Your class is also called LinkedList so it conflicts. If you want to fix it use this line instead. Better still, just have a different name for your class...

List<String> list = new java.util.LinkedList<String>();
like image 28
Adam Avatar answered Oct 09 '22 21:10

Adam


public class LinkedList {

Your class is not defined as generic. I think though that you are trying to use Java's LinkedList, so you should rename your class.

like image 29
James Montagne Avatar answered Oct 09 '22 23:10

James Montagne