Question

I have the following code:

import java.util.*;

public class hello {
  public static void main( String[] args ) {
    Collection c = new ArrayList< Integer >();
    List l = new ArrayList< String >();
  }
}

And get the error in the "List l" line:

hello.java:6: error: incompatible types
    List l = new ArrayList< String >();
             ^
  required: List
  found:    ArrayList<String>
1 error

I am using OpenJDK 1.7:

java version "1.7.0_25"
OpenJDK Runtime Environment (IcedTea 2.3.12) (7u25-2.3.12-4ubuntu3)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)

Edit: When I use List<String> that happens:

hello.java:6: error: type List does not take parameters
    List< String > l = new ArrayList< String >();
        ^
1 error
Was it helpful?

Solution

I have checked this line with my OpenJDK javac:

List l = new ArrayList<String>();

It compiles with no complaints at all.

Based on your second error ("List takes no type arguments"), my guess is that you have either a List type defined in the same package and it is taking precedence over the JDK type, or you are using more import statements which you did not show, such as

import java.awt.*;

where that package contains a List widget.

OTHER TIPS

With the same import statements, run the code below and see the which List class you are importing.

List l;
System.out.println(l.getClass().getName());

Also there might be List class defined as inline in the same source file or another java source file like follows: In this case that class will be used:

package test;

import java.util.*;  //Implicitly imports java.util.list

class Test {
    List list;       //Not java.util.List type but test.List type
    System.out.println(List.class.getName());  //prints test.List
}

class List {}  //Class defined as inline in the same source file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top