chapter 4 generic vector class

15
Chapter 4 Generic Vector Class

Upload: merrill

Post on 20-Feb-2016

49 views

Category:

Documents


3 download

DESCRIPTION

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 ) { - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 4 Generic  Vector Class

Chapter 4 Generic Vector Class

Page 2: 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

Page 3: Chapter 4 Generic  Vector Class

Reading the args array in mainpublic 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 31System.out.println(word+", length "+word.length());

}}

Page 4: Chapter 4 Generic  Vector Class

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

Page 5: Chapter 4 Generic  Vector Class

5

Generic Methods• A generic placeholder (e.g., <T>) 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 <T> void outputNumericArray( T[] array) { for(int i=0; i< array.length; i++)

System.out.println(array[i]); }

Page 6: Chapter 4 Generic  Vector Class

6

Generic Classes

• A generic placeholder (e.g., <T>) 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 <Integer, Double> bill = new PersonGeneric <Integer, Double> (10, 102.56);

Page 7: Chapter 4 Generic  Vector Class

7

Generic Class Code

For the declaration on the previous slide, Integer and Double will be substituted for placeholders T and E respectively

public class PersonGeneric <T, E>{ // 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

Page 8: Chapter 4 Generic  Vector 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<String> wordList; OR Vector wordList

(unsafe ops warning)

Page 9: Chapter 4 Generic  Vector Class

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

Page 10: Chapter 4 Generic  Vector Class

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

Page 11: Chapter 4 Generic  Vector Class

Database concepts

• Load a file of students into a Vector<Student>• Find the student with certain ID• Update a student record, replace back in

Vector•

Page 12: Chapter 4 Generic  Vector Class

What it looks like• Vector<Student> database = new Vector<Student>();• // .. read student100 file into database

database elementCount firstName elementData lastName

IDGPA

firstName lastName

ID

Page 13: Chapter 4 Generic  Vector Class

Another Approach

• Each student record is a Vector of Associations– such as

Page 14: Chapter 4 Generic  Vector Class

More like what we are doing …

Page 15: Chapter 4 Generic  Vector Class

Java Vector vs ArrayList classes• http://www.javaworld.com/javaqa/2001-06/03-qa-0622-vector.html• 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.