Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.

Slides:



Advertisements
Similar presentations
The ArrayList Class and the enum Keyword
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Generics and the ArrayList Class
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
Generics and The ArrayList Class
Generic programming in Java
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L13 (Chapter 21) Generics.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CIT 590 Intro to Programming First lecture on Java.
Java Generics.
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
Introduction to Object-Oriented Programming
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Effective Java: Generics Last Updated: Spring 2009.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
ArrayList, Multidimensional Arrays
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance
CIT 590 Intro to Programming First lecture on Java.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Chapter 4: A Paradigm Program structure Connecting to the Java world Types Access modifiers Lifetime modifiers.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
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.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Practical Session 4 Java Collections. Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Classes - Intermediate
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
(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.
Object Oriented Programming Lecture 2: BallWorld.
Java Generics.
Ch 10- Advanced Object-Oriented Programming Features
Chapter 20 Generic Classes and Methods
Chapter 9 Inheritance and Polymorphism
An Introduction to Java – Part I, language basics
The Building Blocks Classes: Java class library, over 1,800 classes:
Classes, Objects and Methods
Chapter 19 Generics.
Presentation transcript:

Chapter 4 Generic Vector Class

Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes to generic classes – Association class – Vector class

Reading the args array in main public static void main(String[] args) { Vector longWords = new Vector(); int i; for (i = 0; i < args.length; i++) { if (args[i].length() > 4) { longWords.add(args[i]); // line 12 suppose you forget [i] ? }... for (i = 0; i < longWords.size(); i++) { String word = (String)longWords.get(i); // line 31 System.out.println(word+", length "+word.length()); }

4 Generics A language feature for generalizing the type of data a method or class will process The data types are specified by the code – That invokes of the method – That declares an object in the class Method parameter types or class data types can be generic types

5 Generic Methods A generic placeholder (e.g., ) is coded in the method heading and used in the parameter list Invoker’s argument(s) type is substituted for the generic place holder at run time. E.g., generic code to output an array of any type of primitive (int, float, double, char, etc.) public static void outputNumericArray( T[] array) { for(int i=0; i< array.length; i++) System.out.println(array[i]); }

6 Generic Classes A generic placeholder (e.g., ) is coded in the class’ heading and used in the class’ code Types used in an object declaration is substituted for the generic place holder at run time. – Assuming there are two generic types in the class PersonGeneric, an instance declaration would be: PersonGeneric bill = new PersonGeneric (10, );

7 Generic Class Code For the declaration on the previous slide, Integer and Double will be substituted for placeholders T and E respectivelyprevious slide public class PersonGeneric { // definition of the data members private T age; private E weight; // definition of member functions public PersonGeneric( T a, E w ) // the constructor { age = a; weight = w; } public String toString( ) { return( "this person’s age is: " + age + "\n and their weight is: " + weight); } // end of toString method } // end of Person class

Bailey's Structure--Generic Bailey has two versions of his package: – structure for non generic versions – structure5 for generic versions import structure.Vector; non-generic version – can only say Vector wordList; import structure5.Vector; generic version can be used either way Vector wordList; OR Vector wordList (unsafe ops warning)

BlueJ Warning using Generics This happens when you use Java 1.4-style collections (non- generic) with Java 5. The Java 5 compiler produces this warning. In BlueJ, you can switch off this warning in the preferences: Open the 'Miscellaneous' tab in the preferences, and uncheck the option "Show compiler warnings when unsafe collections are used". Alternatively, import structure.Vector NOT structure5.Vector

Errors using Generics Type mismatch – good, want to know about these Sometimes overspecifing types will trigger warning Not all casts are strictly necessary For now be open to modifying your expressions

Database concepts Load a file of students into a Vector Find the student with certain ID Update a student record, replace back in Vector

What it looks like Vector database = new Vector (); //.. read student100 file into database database elementCount firstName elementData lastName ID GPA firstName lastName ID

Another Approach Each student record is a Vector of Associations – such as

More like what we are doing …

Java Vector vs ArrayList classes Both share the same interface (same methods to access, insert) java.util.Vector – “Thread Safe” meaning it can be used in multithreaded apps – When extending array size, it doubles the capacity java.util.ArrayList – “Not Thread Safe” should not be used in multithreaded apps – When extending array size it increases capacity by 50% Overhead from resizing can dampen performance – In general try to estimate the actual size you need in program.