Lists

When you go shopping, you might create a list. As you shop you might check things off your list (remove them from the list). You might search your list to see if something is already on it. You might add to a list. A list holds items in an order.

../_images/lists.jpg

Figure 1: A couple of lists

Lists in Java

Java uses the notion of a list too. It defines the interface List which is in the java.util package. An interface lets you define a type based on what you want it to do, not how it does it. Several classes can implement the same interface and you can pick the one to use that works best in your situation. See http://docs.oracle.com/javase/7/docs/api/java/util/List.html for the Java documentation for the List interface (a portion of this is shown below). All classes in the Java language are organized into packages. A package contains related classes. The String and Object classes are in the java.lang package. The full name for any class is the package name followed by a . and the class name. So the full name for the String class is java.lang.String. The full name for the List interface is java.util.List.

../_images/listInterface.png

Figure 2: The List interface in Java

The Import Statement

If you want to use the short name of List instead of the full name of java.util.List in your code, you will need an import statement. An import statement just tells Java which class you mean when you use a short name. It tells Java where to find the definition of that class. You don’t need an import statement for any class in the package java.lang, like String or Object. You can import just the classes you need from a package as shown below. Import statements have to be the first code in a Java source file.

import java.util.List; // import just the List interface

Or you can import everything at that level in a package. A package is a collection of related classes in Java.

import java.util.*; // import everything at this level

Don’t worry about adding import statements on the AP CS A exam. Any that you need will be provided for you.

8-1-1: Which of the following is true about import statements?