9.18. Chapter Summary

 
[Page 315 ( continued )]

9.9. The ArrayList Class

You can create an array to store objects. But the array's size is fixed once the array is created. Java provides the ArrayList class that can be used to store an unlimited number of objects. Figure 9.6 shows some methods in ArrayList .


[Page 316]
Figure 9.6. An ArrayList stores an unlimited number of objects.

Listing 9.7 is an example of using ArrayList . The program creates an ArrayList using its no-arg constructor (line 4). The add method adds any instance of Object into the list. Since String is a subclass of Object , strings can be added to the list in lines 7 “17. The add method (lines 7 “17) adds an object to the end of list. So, after cityList.add("London") (line 7), the list contains

 [London] 

After cityList.add("New York") (line 9), the list contains

 [London, New York] 

After adding Paris, Toronto, Hong Kong, and Singapore (lines 11 “17), the list would contain

 [London, New York, Paris, Toronto, Hong Kong, Singapore] 

Invoking size() (line 21) returns the size of the list, which is currently 6. Invoking contains("Toronto") (line 23) checks whether the object is in the list. In this case, it returns true , since Toronto is in the list. Invoking indexOf("New York") (line 25) returns the index of the object in the list, which is 1 in this case. If the object is not in the list, it returns ”1 . The isEmpty() method (line 27) checks whether the list is empty. It returns false , since the list is not empty.

The statement cityList.add(2, "Beijing") (line 30) inserts an object to the list at the specified index. After this statement, the list becomes

 [London, New York, Beijing, Paris, Toronto, Hong Kong, Singapore] 

The statement cityList.remove("Toronto") (line 35) removes the object from the list. After this statement, the list becomes

 [London, New York, Beijing, Paris, Hong Kong, Singapore] 

The statement cityList.remove(1) (line 40) removes the object at the specified index from the list. After this statement, the list becomes

 [London, Beijing, Paris, Hong Kong, Singapore] 

The get(index) method (line 45) returns the object at the specified index. Figure 9.7 shows the output of the program.


[Page 317]
Figure 9.7. The program demonstrates how to use ArrayList .

Listing 9.7. TestArrayList.java
(This item is displayed on pages 317 - 318 in the print version)
 1   public class   TestArrayList { 2   public static void   main(String[] args) { 3  // Create a list to store cities  4  java.util.ArrayList cityList =   new   java.util.ArrayList();  5 6  // Add some cities in the list  7  cityList.add(   "  London  "   );  8  // cityList now contains [London]  9 cityList.add(   "New York"   ); 10  // cityList now contains [London, New York]  11  cityList.add(   "Paris"   );  12  // cityList now contains [London, New York, Paris]  13  cityList.add(   "Toronto"   );  14  // cityList now contains [London, New York, Paris, Toronto]  15  cityList.add(   "Hong Kong"   );  16  // contains [London, New York, Paris, Toronto, Hong Kong]  17  cityList.add(   "Singapore"   );  18  // contains [London, New York, Paris, Toronto,  19  // Hong Kong, Singapore]  20 21 System.out.println(   "List size? "   +  cityList.size()  ); 22 System.out.println(   "Is Toronto in the list? "   + 23  cityList.contains(   "Toronto"   )  ); 24 System.out.println(   "The location of New York in the list? "   25 +  cityList.indexOf(   "New York"   )  ); 26 System.out.println(   "Is the list empty? "   + 27  cityList.isEmpty()  );  // Print false  28 29  // Insert a new city at index 2  30 cityList.add(   2, "Beijing"   ); 31  // contains [London, New York, Beijing, Paris, Toronto,  32  // Hong Kong, Singapore]  33 34  // Remove a city from the list  35  cityList.remove(   "Toronto"   );  36  // contains [London, New York, Beijing, Paris,  37  // Hong Kong, Singapore]  38 39  // Remove a city at index 1  40 cityList.remove(   1   ); 41  // contains [London, Beijing, Paris, Hong Kong, Singapore]  42 

[Page 318]
 43  // Display London Beijing Paris Hong Kong Singapore  44   for   (   int   i =     ; i <  cityList.size()  ; i++) 45 System.out.print(cityList.get(i) +   " "   ); 46 System.out.println(); 47 48  // Create a list to store two circles  49 java.util.ArrayList list =   new   java.util.ArrayList(); 50 51  // Add two circles  52 list.add(   new   Circle(   2   )); 53 list.add(   new   Circle(   3   )); 54 55  // Display the area of the first circle in the list  56 System.out.println(   "The area of the circle? "   + 57 ((Circle)list.get(     )).getArea()); 58 } 59 } 

Note

You will get a compilation warning "unchecked operation" in JDK 1.5 when compiling this program. Ignore it and recompile it with the “Xlint option, as shown in Figure 9.5. This warning can be fixed using generic types in Chapter 21, "Generics."


ArrayList objects can be used like arrays, but there are many differences. Table 9.1 lists their similarities and differences.

Table 9.1. Differences and Similarity between Arrays and ArrayList
  Array ArrayList
Creating an array/ArrayList Object[] a = new Object[10] ArrayList list = new ArrayList()
Accessing an element a [index] list.get(index)
Updating an element a [index] = "London"; list.set(index, "London");
Returning size a.length list.size()
Adding a new element   list.add("London")
Inserting a new element   list.add(index, "London")
Removing an element   list.remove(index)
Removing an element   list.remove(Object)
Removing all elements   list.clear()

Once an array is created, its size is fixed. You can access an array element using the square bracket notation (e.g., a[index] ). When an ArrayList is created, its size is 0. You cannot use the get and set method if the element is not in the list. It is easy to add, insert, and remove elements in a list, but it is rather complex to add, insert, and remove elements in an array. You have to write the code to manipulate the array in order to perform these operations.

Note

java.util.Vector is also a class for storing objects, which is very similar to the ArrayList class. All the methods in ArrayList are also available in Vector . The Vector class was introduced in JDK 1.1. The ArrayList class introduced in JDK 1.2 was intended to replace the Vector class.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net