我在debian 5上使用java
java版"1.6.0_20"
Java(TM)SE运行时环境(版本1.6.0_20-b02)
Java HotSpot(TM)客户端VM(版本16.3-b01,混合模式,共享)
为什么以下之间存在差异
情况1:
import java.util.*;
Run Code Online (Sandbox Code Playgroud)
案例2:
import java.util.*;
import java.util.Arrays;
import java.util.List;
Run Code Online (Sandbox Code Playgroud)
为什么第一个案例不包括第二个案例?
代码仅在我明确导入Arrays和List时编译.
码:
import java.util.*;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main (String[] args) {
List<Integer> i = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10));
List<Integer> j = new ArrayList();
ListIterator<Integer> n = i.listIterator(i.size());
while(n.hasPrevious()) {
j.add(n.previous());
}
println(j);
}
static void println(Object o) {
System.out.println(o);
}
static void print(Object o) {
System.out.print(o);
}
}
Run Code Online (Sandbox Code Playgroud)
我在注释掉第2和第3个import语句时得到的错误是:
nattyp@debian:~/dev/java$ javac Test.java
Test.java:7: cannot find symbol
symbol : method asList(int,int,int,int,int,int,int,int,int,int)
location: class Arrays
List<Integer> i = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10));
^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
nattyp@debian:~/dev/java$
Run Code Online (Sandbox Code Playgroud)
我刚刚编译它并且它编译得很好而没有隐式导入,可能你看到过时的缓存或IDE的某些东西.
您是否尝试过从命令行编译?
我有完全相同的版本:

可能你认为警告是错误的.
UPDATE
看起来你Arrays.class在编译目录中有一个文件(可能是之前创建的).这就是显式导入解决问题的原因.尝试将源代码复制到干净的新目录,然后重试.你会看到这次没有错误.或者,清理您的工作目录并删除Arrays.class
和...之间的不同
import java.util.*;
Run Code Online (Sandbox Code Playgroud)
和
import java.util.*;
import java.util.List;
import java.util.Arrays;
Run Code Online (Sandbox Code Playgroud)
当代码引用某些其他List或Arrays(例如,在同一个包中,或者通常也导入)时,这一点变得明显.在第一种情况下,编译器将假定Arrays在同一个包中声明的是在后者中使用的,因为它是专门声明的,java.util.Arrays将使用更具体的.