Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.

Similar presentations


Presentation on theme: "Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class."— Presentation transcript:

1 Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class

2 © 2006 Pearson Addison-Wesley. All rights reserved1-2 The "For Each" Loop The ArrayList class is an example of a collection class Starting with version 5.0, Java has added a new kind of for loop called a for-each or enhanced for loop –This kind of loop has been designed to cycle through all the elements in a collection (like an ArrayList )

3 © 2006 Pearson Addison-Wesley. All rights reserved1-3 A for-each Loop Used with an ArrayList (Part 1 of 3)

4 © 2006 Pearson Addison-Wesley. All rights reserved1-4 A for-each Loop Used with an ArrayList (Part 2 of 3)

5 © 2006 Pearson Addison-Wesley. All rights reserved1-5 A for-each Loop Used with an ArrayList (Part 3 of 3)

6 © 2006 Pearson Addison-Wesley. All rights reserved1-6 Golf Score Program (Part 1 of 6)

7 © 2006 Pearson Addison-Wesley. All rights reserved1-7 Golf Score Program (Part 2 of 6)

8 © 2006 Pearson Addison-Wesley. All rights reserved1-8 Golf Score Program (Part 3 of 6)

9 © 2006 Pearson Addison-Wesley. All rights reserved1-9 Golf Score Program (Part 4 of 6)

10 © 2006 Pearson Addison-Wesley. All rights reserved1-10 Golf Score Program (Part 5 of 6)

11 © 2006 Pearson Addison-Wesley. All rights reserved1-11 Golf Score Program (Part 6 of 6)

12 © 2006 Pearson Addison-Wesley. All rights reserved1-12 Tip: Use trimToSize to Save Memory An ArrayList automatically increases its capacity when needed –However, the capacity may increase beyond what a program requires –In addition, although an ArrayList grows automatically when needed, it does not shrink automatically If an ArrayList has a large amount of excess capacity, an invocation of the method trimToSize will shrink the capacity of the ArrayList down to the size needed

13 © 2006 Pearson Addison-Wesley. All rights reserved1-13 Pitfall: The clone method Makes a Shallow Copy When a deep copy of an ArrayList is needed, using the clone method is not sufficient –Invoking clone on an ArrayList object produces a shallow copy, not a deep copy In order to make a deep copy, it must be possible to make a deep copy of objects of the base type –Then a deep copy of each element in the ArrayList can be created and placed into a new ArrayList object

14 © 2006 Pearson Addison-Wesley. All rights reserved1-14 The Vector Class The Java standard libraries have a class named Vector that behaves almost exactly the same as the class ArrayList In most situations, either class could be used –However the ArrayList class is newer, and is becoming the preferred class

15 © 2006 Pearson Addison-Wesley. All rights reserved1-15 Parameterized Classes and Generics The class ArrayList is a parameterized class It has a parameter, denoted by Base_Type, that can be replaced by any reference type to obtain a class for ArrayLists with the specified base type Starting with version 5.0, Java allows class definitions with parameters for types –These classes that have type parameters are called parameterized class or generic definitions, or, simply, generics

16 © 2006 Pearson Addison-Wesley. All rights reserved1-16 Nonparameterized ArrayList and Vector Classes The ArrayList and Vector classes discussed here have a type parameter for the base type There are also ArrayList and Vector classes with no parameter whose base type is Object –These classes are left over from earlier versions of Java

17 © 2006 Pearson Addison-Wesley. All rights reserved1-17 Example: Nonparameterized ArrayList //Different objects can be added to the collection. import java.util.ArrayList; import java.util.Collections; class ArrayListOfAllKinds { public static void main(String args []) { ArrayList theArray = new ArrayList(); theArray.add(new Double(3.7)); theArray.add(new Boolean(true)); theArray.add(new Integer(19)); theArray.add(new String(";-)")); System.out.print("["); for(int i=0;i< theArray.size();i++) System.out.print(theArray.get(i)+" "); System.out.println("]"); // toString() method is defined for the ArrayList class System.out.println(theArray); } [3.7 true 19 ;-) ] [3.7, true, 19, ;-)]

18 © 2006 Pearson Addison-Wesley. All rights reserved1-18 Example: List of Lists // arrayList objects can be added to other arrayList objects. import java.util.ArrayList; class ListOfLists { public static void main(String s[]){ ArrayList ics=new ArrayList(); ArrayList coe=new ArrayList(); String[] icsBasics={"ics102", "ics103", "ics201", "ics202"}; String[] coeBasics={"coe200", "ics102", "ics201", "ics202"}; for (int i=0; i<icsBasics.length; i++){ ics.add(icsBasics[i]); coe.add(coeBasics[i]); } ArrayList kfupm=new ArrayList(); kfupm.add(ics); kfupm.add(coe); System.out.println(kfupm); } [[ics102, ics103, ics201, ics202], [coe200, ics102, ics201, ics202]]


Download ppt "Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class."

Similar presentations


Ads by Google