Java import statement

I use Java on Debian 5

Why is there a difference between the following?

Case 1:

import java.util.*;

Case 2:

import java.util.*;
import java.util.Arrays;
import java.util.List;

Why does the first case not include the second case?

When I explicitly import arrays and lists, the code only compiles

Code:

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

}

When I annotate the second and third import statements, the error I get is:

nattyp@debian:~/dev/java$javac Test.java
Test.java:7: cannot find symbol
symbol  : method asList(int,int,int)
location: class Arrays
                List<Integer> i = new ArrayList(Arrays.asList(1,10));
                                                      ^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
nattyp@debian:~/dev/java$

Solution

I just compile it. It compiles without implicit import. Maybe you see an old cache or something in your ide

Are you trying to compile from the command line?

I have exactly the same version:

Maybe you're thinking that this warning is a mistake

UPDATE

It appears that the directory you are trying to compile (which may have been created earlier) has an arrays Class file This is why explicit import solves this problem Try copying the source code to a clean new directory and try again You will see that there are no mistakes this time Alternatively, clean up your working directory and delete arrays class

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
分享
二维码
< <上一篇
下一篇>>