java.util.vector brian toone 10/3/07 updated 10/10/07

8
java.util.Vector Brian Toone 10/3/07 Updated 10/10/07

Upload: randell-oneal

Post on 16-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07

java.util.Vector

Brian Toone10/3/07

Updated 10/10/07

Page 2: Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07

What is a Vector object?• Think of it as a really convenient array• Consider:

• What if we now want to add a 6th element?• What if we want to check to see if a particular

element is contained in the array?• What if we want to sort the elements?

Page 3: Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07

The nice things about Vector

• Vector allows you to add elements “on-the-fly”• Vector provides a method for automatically

searching for a particular element in the array• The java.util.Collections class provides a static

method for sorting the elements in the array

Page 4: Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07

Vector Examples

• Eclipse• Solution to “We are the champions”• Solution to “Village People”

Page 5: Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07

java.util.Collections

• The Java Tutorial (Sun), Trail: Collectionshttp://java.sun.com/docs/books/tutorial/collections/index.html

• Library distributed with latest (> 1.4) versions of the JDK

• Consists of– Interfaces (Set, List, Queue, Map, SortedSet, SortedMap)– Implementations (HashSet, HashMap, ArrayList,

LinkedList)– Algorithms (Sorting, Shuffling, Frequency, Composition)– Vector???

Page 6: Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07

Example Algorithm: sort

• Algorithms provided as static methods in a class called Collections.

• Simple example:// Add as many elements as we want!

Vector<Integer> v = new Vector<Integer>();

v.add(35);

v.add(85);

v.add(22);

v.add(99);

v.add(68);

v.add(73);

// Sort the array

Collections.sort(v);

Page 7: Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07

Example Interface: map

• Think of this as an associative array• Associative arrays are found in some scripting

languages such as PHP:

$blogTitle = sanitize($_REQUEST['blogTitle']);

$blogTags = sanitize($_REQUEST['blogTags']);

$blogSummary = sanitize($_REQUEST['blogSummary']);

$htmlArea = sanitize($_REQUEST['htmlarea']);

Page 8: Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07

Let’s see an example in Java