23.10. Review Questions

 
[Page 703 ( continued )]

21.8. Avoiding Unsafe Raw Types

Raw types are unsafe in JDK 1.5. If you try to use them, you will get a compilation warning "unchecked operation." To fix it, instantiate generic types. For example, Listing 9.7, TestArrayList.java, uses an array list to store strings. The compiler reports unchecked operation warnings on the add method. These warnings can be avoided if you revise the program, as shown in Listing 21.9. A list to store strings is created in line 6, and strings are added to the list in lines 9 “14. A list to store circles is created in line 39, and circles are added to the list in lines 42 “43.


[Page 704]
Listing 21.9. TestArrayListNew.java
 1   import   java.util.*;  2  3   public class   TestArrayListNew {  4   public static void   main(String[] args) {  5  // Create a list to store cities  6  ArrayList<String> cityList =   new   ArrayList<String>();  7  8  // Add some cities in the list  9  cityList.add(   "London"   );  10     cityList.add(   "New York"   ); 11     cityList.add(   "Paris"   ); 12     cityList.add(   "Toronto"   ); 13     cityList.add(   "Hong Kong"   ); 14     cityList.add(   "Singapore"   ); 15 16     System.out.println(   "List size? "   + cityList.size()); 17     System.out.println(   "Is Toronto in the list? "   + 18       cityList.contains(   "Toronto"   )); 19     System.out.println(   "The location of New York in the list? "   20       + cityList.indexOf(   "New York"   )); 21     System.out.println(   "Is the list empty? "   + 22       cityList.isEmpty());  // Print false  23 24  // Insert a new city at index 2  25     cityList.add(   2   ,   "Beijing"   ); 26 27  // Remove a city from the list  28     cityList.remove(   "Toronto"   ); 29 30  // Remove a city at index 1  31     cityList.remove(   1   ); 32 33  // Display London Beijing Paris Hong Kong Singapore  34   for   (   int   i =     ; i < cityList.size(); i++) 35       System.out.print(cityList.get(i) +   " "   ); 36     System.out.println(); 37 38  // Create a list to store two circles  39  ArrayList<Circle> list =   new   ArrayList<Circle>();  40 41  // Add a circle and a cylinder  42  list.add(   new   Circle(   2   ));  43     list.add(   new   Circle(   3   )); 44 45  // Display the area of the first circle in the list  46     System.out.println(   "The area of the circle? "   + 47       ((Circle)list.get(     )).findArea()); 48   } 49 } 

The sort method in Listing 10.5, GenericSort.java, has "unchecked operation" warning. It can be fixed as shown in Listing 21.10. The generic type E is a subtype of Comparable<E> . When invoking the sort method, you must pass an array of comparable objects.


[Page 705]
Listing 21.10. GenericSortNew.java
 1   public class   GenericSortNew {  2  /** Sort an array of comparable objects */  3   public static    <E   extends   Comparable<E>>    void   sort(  E  [] list) {  4  E currentMax;  5   int   currentMaxIndex;  6  7   for   (   int   i = list.length -   1   ; i >=   1   ; i) {  8  // Find the maximum in the list[0..i]  9       currentMax = list[i]; 10       currentMaxIndex = i; 11 12   for   (   int   j = i -   1   ; j >=     ; j) { 13   if   (currentMax.compareTo(list[j]) <     ) { 14           currentMax = list[j]; 15           currentMaxIndex = j; 16         } 17       } 18 19  // Swap list[i] with list[currentMaxIndex] if necessary;  20   if   (currentMaxIndex != i) { 21         list[currentMaxIndex] = list[i]; 22         list[i] = currentMax; 23       } 24     } 25   } 26 27   public static void   main(String[] args) { 28  // Create a String array  29     String[] stringArray = {   "Tom"   ,   "John"   ,   "Fred"   }; 30 31  sort(stringArray);  32 33   for   (   int   i =     ; i < stringArray.length; i++) 34       System.out.print(stringArray[i] +   " "   ); 35   } 36 } 

 


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