Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't importing java.util.* include Arrays and Lists?

Tags:

java

import

I am using java on debian 5

java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

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 doesnt the first case cover the second case?

The code only compiles when I import Arrays and List explicitly.

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

}

The error I get when I comment out the 2nd and 3rd import statements are:

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$
like image 749
nattyP Avatar asked Dec 28 '10 02:12

nattyP


People also ask

What does import Java util * import?

Java util package contains collection framework, collection classes, classes related to date and time, event model, internationalization, and miscellaneous utility classes. On importing this package, you can access all these classes and methods.

Do arrays need to be imported in Java?

The Arrays. toString() would not work without import java.

Are arrays in Java Util?

The Arrays class in java. util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of Object class.

Does ArrayList need to be imported?

In order to use the ArrayList class, the ArrayList class needs to be imported from the java util package.


3 Answers

I have just compile it and it compiles fine without the implicit import, probably you're seeing a stale cache or something of your IDE.

Have you tried compiling from the command line?

I have the exact same version:

here it is

Probably you're thinking the warning is an error.

UPDATE

It looks like you have a Arrays.class file in the directory where you're trying to compile ( probably created before ). That's why the explicit import solves the problem. Try copying your source code to a clean new directory and try again. You'll see there is no error this time. Or, clean up your working directory and remove the Arrays.class

like image 130
OscarRyz Avatar answered Oct 22 '22 22:10

OscarRyz


The difference between

import java.util.*;

and

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

becomes apparent when the code refers to some other List or Arrays (for example, in the same package, or also imported generally). In the first case, the compiler will assume that the Arrays declared in the same package is the one to use, in the latter, since it is declared specifically, the more specific java.util.Arrays will be used.

like image 43
Avi Avatar answered Oct 22 '22 22:10

Avi


Case 1 should have worked. I don't see anything wrong. There may be some other problems. I would suggest a clean build.

like image 1
fastcodejava Avatar answered Oct 22 '22 23:10

fastcodejava