an introduction to the java vector class chris giannella

18
An Introduction to the Java Vector Class Chris Giannella

Upload: sherman-hancock

Post on 18-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: An Introduction to the Java Vector Class Chris Giannella

An Introduction to the Java Vector Class

Chris Giannella

Page 2: An Introduction to the Java Vector Class Chris Giannella

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

THE END

Thank you for your attention

Page 18: An Introduction to the Java Vector Class Chris Giannella

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