java.util.vector brian toone 10/3/07 updated 10/10/07
TRANSCRIPT
![Page 1: Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07](https://reader036.vdocument.in/reader036/viewer/2022082407/56649f065503460f94c1bf4e/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082407/56649f065503460f94c1bf4e/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082407/56649f065503460f94c1bf4e/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082407/56649f065503460f94c1bf4e/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082407/56649f065503460f94c1bf4e/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082407/56649f065503460f94c1bf4e/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082407/56649f065503460f94c1bf4e/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022082407/56649f065503460f94c1bf4e/html5/thumbnails/8.jpg)
Let’s see an example in Java