Why can’t the Java 7 and eclipse 3.8 compilers compile JDK code with the new Java 7 Diamond operator?
•
Java
import java.util.*;
import java.util.*; public class SimpleArrays { @SafeVarargs public static <T> List<T> asList( T... a ) { return new ArrayList<>( a ); } }
Aslist() is taken from Java util. Oracle JDK implementation of arrays
The mistake is
error: cannot infer type arguments for ArrayList<> return new ArrayList<>( a ); 1 error
How can this work? Oracle uses the same compiler as ours
Solution
Note: Java util. The ArrayList used in the arrays class is not Java util. ArrayList, but nested class java util. Arrays. ArrayList.
In particular, this class has a constructor that takes t [] as a parameter, which Java util. ArrayList does not
Copying this class also works
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码