an introduction to the java vector class chris giannella
TRANSCRIPT
![Page 1: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/1.jpg)
An Introduction to the Java Vector Class
Chris Giannella
![Page 2: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/2.jpg)
Outline
• What is the difference between vectors and arrays? Why use vectors?
• What methods do vectors provide?• Example: a searchable string database
class
Programming.Java An Introduction to Programming Using Java (2nd ed.), Decker and Hirshfield
![Page 3: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/3.jpg)
What Are Vectors?
• A linear collection of data elements
The cat in the hat
element 0 element 4
![Page 4: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/4.jpg)
Differences Between Vectors and Arrays
• Vectors are dynamic– Useful because size need not be specified in
advance
• Data elements are always of type Object
• Provide a suite of access methods
![Page 5: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/5.jpg)
Some Inspector Methods
Given vector object V:
• int V.size() – number of elements in V
• int V.indexOf(Object obj) – Index of the first occurrence of obj (-1 if no
occurrences)
![Page 6: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/6.jpg)
An Insertion Method
• void V.insertElementAt(Object obj, int idx) – put obj at index idx, shifting other elements if
necessary– an error occurs if idx > V.size()
• Example: String str = “Hat”;V.insertElementAt(str,V.size());
Inserts str at the end of V
![Page 7: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/7.jpg)
A Subtlety
void V.insertElementAt(Object obj, int idx)
V.insertElementAt(str,V.size());
Automatic Typecast
![Page 8: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/8.jpg)
A Deletion Method
• void V.removeElementAt(int idx) – delete the element at index idx, shifting other
elements if necessary– an error occurs if idx > V.size()-1
![Page 9: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/9.jpg)
Example: A Searchable String Database Class
• Should allow– insertion of strings– deletion of strings– search for a string
![Page 10: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/10.jpg)
Define a Vector Subclass
class StringDatabase extends Vector
{
Vector D; // The contents
// Specialized methods added here next
}
![Page 11: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/11.jpg)
Define specialized methodsclass StringDatabase extends Vector{
Vector D; // The contents
boolean insert(String str,int idx){
if (idx > D.size())return(false);
else{
D.insertElementAt(str,idx);return(true);
} }
}
![Page 12: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/12.jpg)
class StringDatabase extends Vector{
Vector D; // The contents
.
.
.void delete(String str) {
int idx = D.indexOf(str);if (idx != -1)
D.removeElementAt(idx);}
}
![Page 13: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/13.jpg)
class StringDatabase extends Vector
{
Vector D; // The contents
.
.
.int find(String str)
{
return(D.indexOf(str));
}}
![Page 14: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/14.jpg)
Example
StringDatabase SD = new StringDatabase();
// Creates an initially empty database
SD.insert(“The”,SD.size());
SD.insert(“cat”,SD.size());
SD.insert(“in”,SD.size());
The cat in
![Page 15: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/15.jpg)
Example (cont.)
SD.delete(“cat”);
SD.find(“in”) Returns 1
SD.find(“cat”) Returns -1
The in
![Page 16: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/16.jpg)
Wrap-Up
• Described the difference between Java Vectors and Arrays – dynamic size (motivation for use)
• Described some Vector methods
• Gave an example– searchable string database class
![Page 17: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/17.jpg)
THE END
Thank you for your attention
![Page 18: An Introduction to the Java Vector Class Chris Giannella](https://reader036.vdocument.in/reader036/viewer/2022073120/56649d0c5503460f949e093a/html5/thumbnails/18.jpg)
A Tangent: Data Types and Typecasting
• Primitive types– int, boolean, char, …
• Non-primitive types: any class– String, Vector, Object, …
• Object is a superclass of all Java library classes
• Typecasting– Any Java library class Object