Unformatted text preview:

Chapter 14Introduction to GenericsThe ArrayList ClassSlide 4Slide 5Using the ArrayList ClassSlide 7Slide 8Slide 9Tip: Summary of Adding to an ArrayListTip: Summary of Adding to an ArrayListSlide 12Methods in the Class ArrayListSome Methods in the Class ArrayList (Part 1 of 11)Some Methods in the Class ArrayList (Part 2 of 11)Some Methods in the Class ArrayList (Part 3 of 11)Some Methods in the Class ArrayList (Part 4 of 11)Some Methods in the Class ArrayList (Part 5 of 11)Some Methods in the Class ArrayList (Part 6 of 11)Some Methods in the Class ArrayList (Part 7 of 11)Some Methods in the Class ArrayList (Part 8 of 11)Some Methods in the Class ArrayList (Part 9 of 11)Some Methods in the Class ArrayList (Part 10 of 11)Some Methods in the Class ArrayList (Part 11 of 11)The "For Each" LoopA for-each Loop Used with an ArrayList (Part 1 of 3)A for-each Loop Used with an ArrayList (Part 2 of 3)A for-each Loop Used with an ArrayList (Part 3 of 3)Golf Score Program (Part 1 of 6)Golf Score Program (Part 2 of 6)Golf Score Program (Part 3 of 6)Golf Score Program (Part 4 of 6)Golf Score Program (Part 5 of 6)Golf Score Program (Part 6 of 6)Parameterized Classes and GenericsGenericsSlide 37A Class Definition with a Type ParameterClass Definition with a Type ParameterA Generic Ordered Pair Class (Part 1 of 4)A Generic Ordered Pair Class (Part 2 of 4)A Generic Ordered Pair Class (Part 3 of 4)A Generic Ordered Pair Class (Part 4 of 4)Using generic ordered pair classUsing Our Ordered Pair Class (Part 1 of 3)Using Our Ordered Pair Class (Part 2 of 3)Using Our Ordered Pair Class (Part 3 of 3)Pitfall: A Generic Constructor Name Has No Type ParameterPitfall: A Primitive Type Cannot be Plugged in for a Type ParameterPitfall: A Type Parameter Cannot Be Used Everywhere a Type Name Can Be UsedPitfall: An Instantiation of a Generic Class Cannot be an Array Base TypeUsing Our Ordered Pair Class and Automatic Boxing (Part 1 of 3)Using Our Ordered Pair Class and Automatic Boxing (Part 2 of 3)Using Our Ordered Pair Class and Automatic Boxing (Part 3 of 3)Pitfall: A Class Definition Can Have More Than One Type ParameterMultiple Type Parameters (Part 1 of 4)Multiple Type Parameters (Part 2 of 4)Multiple Type Parameters (Part 3 of 4)Multiple Type Parameters (Part 4 of 4)Using a Generic Class with Two Type Parameters (Part 1 of 2)Using a Generic Class with Two Type Parameters (Part 2 of 2)Bounds for Type ParametersSlide 63A Bounded Type ParameterTip: Generic InterfacesGeneric MethodsSlide 67Generic methodsSlide 69Inheritance with Generic ClassesA Derived Generic Class (Part 1 of 2)A Derived Generic Class (Part 2 of 2)Using UnorderedPair (Part 1 of 2)Using UnorderedPair (Part 2 of 2)Sample ProblemChapter 14Generics and the ArrayList ClassCopyright © 2008 Pearson Addison-Wesley. All rights reservedIntroduction to Generics•Beginning with version 5.0, Java allows class and method definitions that include parameters for types•Such definitions are called generics–Generic programming with a type parameter enables code to be written that applies to any class 14-2Copyright © 2008 Pearson Addison-Wesley. All rights reservedThe ArrayList Class•ArrayList is a class in the standard Java libraries–Unlike arrays, which have a fixed length once they have been created, an ArrayList is an object that can grow and shrink while your program is running•In general, an ArrayList serves the same purpose as an array, except that an ArrayList can change length while the program is running14-3Copyright © 2008 Pearson Addison-Wesley. All rights reservedThe ArrayList Class•The class ArrayList is implemented using an array as a private instance variable–When this hidden array is full, a new larger hidden array is created and the data is transferred to this new array14-4Copyright © 2008 Pearson Addison-Wesley. All rights reservedThe ArrayList Class•Why not always use an ArrayList instead of an array? 1. An ArrayList is less efficient than an array2. It does not have the convenient square bracket notation3. The base type of an ArrayList must be a class type (or other reference type): it cannot be a primitive type–This last point is less of a problem now that Java provides automatic boxing and unboxing of primitives14-5Copyright © 2008 Pearson Addison-Wesley. All rights reservedUsing the ArrayList Class•In order to make use of the ArrayList class, it must first be imported from the package java.util•An ArrayList is created and named in the same way as object of any class, except that you specify the base type as follows:ArrayList<BaseType> aList = new ArrayList<BaseType>();14-6Copyright © 2008 Pearson Addison-Wesley. All rights reservedUsing the ArrayList Class•An initial capacity can be specified when creating an ArrayList as well–The following code creates an ArrayList that stores objects of the base type String with an initial capacity of 20 itemsArrayList<String> list = new ArrayList<String>(20);–Specifying an initial capacity does not limit the size to which an ArrayList can eventually grow•Note that the base type of an ArrayList is specified as a type parameter14-7Copyright © 2008 Pearson Addison-Wesley. All rights reservedUsing the ArrayList Class•The add method is used to set an element for the first time in an ArrayListlist.add("something");–The method name add is overloadedThere is also a two argument version that allows an item to be added at any currently used index position or at the first unused positionlist.add(3, “boat”);14-8Copyright © 2008 Pearson Addison-Wesley. All rights reservedUsing the ArrayList Class•The size method is used to find out how many indices already have elements in the ArrayListint howMany = list.size();•The set method is used to replace any existing element, and the get method is used to access the value of any existing elementlist.set(index, "something else");String thing = list.get(index);14-9Copyright © 2008 Pearson Addison-Wesley. All rights reservedTip: Summary of Adding to an ArrayList•The add method is usually used to place an element in an ArrayList position for the first time (at an ArrayList index) •The simplest add method has a single parameter for the element to be added, and adds an element at the next unused index, in order14-10Copyright © 2008 Pearson Addison-Wesley. All rights reservedTip: Summary of Adding to an ArrayList •An element can be added at an already occupied list position by using the two-parameter version of


View Full Document

SJU CSC 3405 - Lecture notes

Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?