1 / 15

Chapter 4 Generic Vector Class

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

merrill
Download Presentation

Chapter 4 Generic Vector Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

  7. Generic Class Code public class PersonGeneric <T, E> { // definition of the data members privateT age; privateE weight; // definition of member functions public PersonGeneric(T a, Ew ) // 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 For the declaration on the previous slide, Integer and Double will be substituted for placeholders T and E respectively

  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<String> 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<Student> • Find the student with certain ID • Update a student record, replace back in Vector

  12. What it looks like • Vector<Student> database = new Vector<Student>(); • // .. read student100 file into database database elementCountfirstName elementDatalastName 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 vsArrayList 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.

More Related