Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes."— Presentation transcript:

1 Chapter 4 Generic Vector Class

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

3 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 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 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 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, 102.56);

7 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

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

9 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

10 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

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

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

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

14 More like what we are doing …

15 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.


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

Similar presentations


Ads by Google