Chapter 4 Generic Vector Class

advertisement
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());
}
}
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
4
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]);
}
5
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);
6
Generic Class Code
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
For the declaration on the previous slide, Integer and Double will be
substituted for placeholders T and E respectively
7
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)
BlueJ Warning using Generics
• This happens when you use Java 1.4-style collections (nongeneric) 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<Student>
• Find the student with certain ID
• Update a student record, replace back in
Vector
•
What it looks like
• Vector<Student> database = new Vector<Student>();
• // .. read student100 file into database
database
elementCount
elementData
firstName
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
• 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