The For Each Loop The Array List class

















- Slides: 17

The "For Each" Loop • The Array. List 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 Array. List) © 2006 Pearson Addison-Wesley. All rights reserved 1

A for-each Loop Used with an Array. List (Part 1 of 3) © 2006 Pearson Addison-Wesley. All rights reserved 2

A for-each Loop Used with an Array. List (Part 2 of 3) © 2006 Pearson Addison-Wesley. All rights reserved 3

A for-each Loop Used with an Array. List (Part 3 of 3) © 2006 Pearson Addison-Wesley. All rights reserved 4

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

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

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

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

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

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

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

Pitfall: The clone method Makes a Shallow Copy • When a deep copy of an Array. List is needed, using the clone method is not sufficient – Invoking clone on an Array. List 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 Array. List can be created and placed into a new Array. List object © 2006 Pearson Addison-Wesley. All rights reserved 12

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

Parameterized Classes and Generics • The class Array. List 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 Array. Lists 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 © 2006 Pearson Addison-Wesley. All rights reserved 14

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

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

Example: List of Lists // array. List objects can be added to other array. List objects. import java. util. Array. List; class List. Of. Lists { public static void main(String s[]){ Array. List ics=new Array. List(); Array. List coe=new Array. List(); String[] ics. Basics={"ics 102", "ics 103", "ics 201", "ics 202"}; String[] coe. Basics={"coe 200", "ics 102", "ics 201", "ics 202"}; for (int i=0; i<ics. Basics. length; i++){ ics. add(ics. Basics[i]); coe. add(coe. Basics[i]); } Array. List kfupm=new Array. List(); kfupm. add(ics); kfupm. add(coe); System. out. println(kfupm); } [[ics 102, ics 103, ics 201, ics 202], [coe 200, ics 102, ics 201, ics 202]] } © 2006 Pearson Addison-Wesley. All rights reserved 17