简体   繁体   中英

“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));
        }
    }
}

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.

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>();
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.

You placed your main method in a class called LinkedList , which takes precedence over the built-in LinkedList class in the compiler's name resolution algorithm. In other words, because your main method is inside a class called LinkedList , the compiler takes the name LinkedList to mean your LinkedList class instead of java.util.LinkedList . There are two easy fixes:

  1. Rename your class
  2. Instead of writing

    List list = new LinkedList();

    write

    List list = new java.util.LinkedList();

I highly recommend the first option. Since your LinkedList class doesn't actually represent the linked list data structure, the name on your class is confusing to someone reading the code. Maybe call your class LinkedListTest instead ie your file could be

import java.util.*;

public class LinkedListTest {
    // Implementation of main and helper methods
}
public class LinkedList {

You named your own class LinkedList , so it's taking precedence over the java.util.LinkedList .

Either change the name of the class to something else or do new java.util.LinkedList<String>() to make it explicit which one you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM