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

Slides:



Advertisements
Similar presentations
Generics and the ArrayList Class
Advertisements

Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 23 : Generics King Fahd University of Petroleum & Minerals College of Computer Science.
Generics and The ArrayList Class
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part II.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
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 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
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,
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
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.
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.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
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.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
CSE 1201 Object Oriented Programming ArrayList 1.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Chapter 1 Object Orientation: Objects and Classes.
Java Generics.
Computer Engineering Department Islamic University of Gaza
Java Generics.
Comp 249 Programming Methodology
Chapter 3 Introduction to Classes, Objects Methods and Strings
CMSC 202 Generics.
Comp 249 Programming Methodology
CMSC 202 Generics.
Chapter 4 Constructors Section 4.4
Presentation transcript:

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

© 2006 Pearson Addison-Wesley. All rights reserved1-2 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

© 2006 Pearson Addison-Wesley. All rights reserved1-3 Generics Classes and methods can have a type parameter –A type parameter can have any reference type (i.e., any class type) plugged in for the type parameter –When a specific type is plugged in, this produces a specific class type or method –Traditionally, a single uppercase letter is used for a type parameter, but any non-keyword identifier may be used

© 2006 Pearson Addison-Wesley. All rights reserved1-4 Generics A class definition with a type parameter is stored in a file and compiled just like any other class Once a parameterized class is compiled, it can be used like any other class –However, the class type plugged in for the type parameter must be specified before it can be used in a program –Doing this is said to instantiate the generic class Sample object = new Sample ();

© 2006 Pearson Addison-Wesley. All rights reserved1-5 A Class Definition with a Type Parameter

© 2006 Pearson Addison-Wesley. All rights reserved1-6 Class Definition with a Type Parameter A class that is defined with a parameter for a type is called a generic class or a parameterized class –The type parameter is included in angular brackets after the class name in the class definition heading –Any non-keyword identifier can be used for the type parameter, but by convention, the parameter starts with an uppercase letter –The type parameter can be used like other types used in the definition of a class

© 2006 Pearson Addison-Wesley. All rights reserved1-7 A Generic Ordered Pair Class (Part 1 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved1-8 A Generic Ordered Pair Class (Part 2 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved1-9 A Generic Ordered Pair Class (Part 3 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved1-10 A Generic Ordered Pair Class (Part 4 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved1-11 Using Our Ordered Pair Class (Part 1 of 3)

© 2006 Pearson Addison-Wesley. All rights reserved1-12 Using Our Ordered Pair Class (Part 2 of 3)

© 2006 Pearson Addison-Wesley. All rights reserved1-13 Using Our Ordered Pair Class (Part 3 of 3)

© 2006 Pearson Addison-Wesley. All rights reserved1-14 Pitfall: A Generic Constructor Name Has No Type Parameter Although the class name in a parameterized class definition has a type parameter attached, the type parameter is not used in the heading of the constructor definition public Pair() A constructor can use the type parameter as the type for a parameter of the constructor, but in this case, the angular brackets are not used public Pair(T first, T second) However, when a generic class is instantiated, the angular brackets are used Pair pair = new Pair ("Happy", "Day");

© 2006 Pearson Addison-Wesley. All rights reserved1-15 Pitfall: A Primitive Type Cannot be Plugged in for a Type Parameter The type plugged in for a type parameter must always be a reference type –It cannot be a primitive type such as int, double, or char –However, now that Java has automatic boxing, this is not a big restriction –Note: reference types can include arrays

© 2006 Pearson Addison-Wesley. All rights reserved1-16 Pitfall: A Type Parameter Cannot Be Used Everywhere a Type Name Can Be Used Within the definition of a parameterized class definition, there are places where an ordinary class name would be allowed, but a type parameter is not allowed In particular, the type parameter cannot be used in simple expressions using new to create a new object –For instance, the type parameter cannot be used as a constructor name or like a constructor: T object = new T(); T[] a = new T[10];

© 2006 Pearson Addison-Wesley. All rights reserved1-17 Pitfall: An Instantiation of a Generic Class Cannot be an Array Base Type Arrays such as the following are illegal: Pair [] a = new Pair [10]; –Although this is a reasonable thing to want to do, it is not allowed given the way that Java implements generic classes