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

Slides:



Advertisements
Similar presentations
Review Generics and the ArrayList Class
Advertisements

Generics and the ArrayList Class
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 23 : Generics King Fahd University of Petroleum & Minerals College of Computer Science.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Iterators An iterator is an object that is used with a collection to provide sequential access to.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L13 (Chapter 21) Generics.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Unit 301 Java Collections Framework: Classes I JCF Class Hierarchy The ArrayList Class The Collections Class.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Iterators in Java. Lecture Objectives To understand the concepts of Java iterators To understand the differences between the Iterator and ListIterator.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Introduction to Generics
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CSE 1201 Object Oriented Programming ArrayList 1.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
CMSC 202 ArrayList Aug 9, 2007.
CMSC 202 ArrayList Aug 9, 2007.
Dynamic Data Structures and Generics
Comp 249 Programming Methodology
CMSC 202 ArrayList Aug 9, 2007.
slides created by Ethan Apter
Classes, Objects and Methods
Presentation transcript:

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

© 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 )

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

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

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

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

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

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

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

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

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

© 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

© 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

© 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

© 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

© 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

© 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, ;-)]

© 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]]