CS2_28_UsefulTools - CS2 Module 28 Category: Elements of...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
CS2 Module 28 Category: Elements of Java Topic: Useful Tools Objectives Enumeration Vector StringTokenizer LinkedList Stack GregorianCalendar
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 28 Elements of Java Useful Tools: Enumeration, Vector, StringTokenizer, LinkedList, Stack, GregorianCalendar
Background image of page 2
java.util Useful java utilities included in java.util: java.util.Enumeration -- an interface allowing for a standardized technique for iterating through a collection java.util.Vector -- a linked list, with random access java.util.StringTokenizer -- a tool for make ‘substrings’ or tokens from a String java.util.LinkedList -- a powerful doubly linked list implementation java.util.Stack -- a subclass of Vector adding typical stack methods java.util.GregorianCalendar -- an implementation of the abstract Calendar Class implementing the widely used Gregorian Calendar
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Enumeration Examining the Java API reveals that the designers tend to standardize ways of doing things An example is the simple task of iterating through a collection
Background image of page 4
Thinking OO Suppose we have a collection class* like a LinkedList We want to be able to iterate through the entire list and get a reference to each data item How can we do this? * Bag, Set, LinkedList, Queue, Stack, etc.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
One way Add another variable to the class. ..call it current and make it a reference to a ListNode Add some methods nextElement() Returns current.getData() Makes current = current.getNext() hasMoreElements() Returns true if current != null reset() Sets current equal to head
Background image of page 6
Wait a minute! Suppose someone makes changes to the list. ..how do we handle the situation where current happens to be pointing to a deleted node? Does a LinkedList really need to have an extra reference? Are we really outside what a LinkedList needs to be able to do?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Solution Here is our typical LinkedList LinkedList head addFront deleteFront addBack etc. ListNode data next ListNode data next
Background image of page 8
Solution When we need to traverse a LinkedList we'll create a special object. For now call it a Traverser LinkedList head deleteFront addBack etc. ListNode data next ListNode data next Traverser current hasMoreElements nextElement
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Reset? Does it need a reset method? LinkedList head deleteFront addBack etc. ListNode data next ListNode data next Traverser current hasMoreElements nextElement
Background image of page 10
No!!! If we need to reset we can just create another traverser! LinkedList head deleteFront addBack etc. ListNode data next ListNode data next Traverser current hasMoreElements nextElement n e w o
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Where? So where does this "Traverser" get created? We could add a method to the LinkedList class which would make a new Traverser and pass it the value of head public void Traverser elements() { return new Traverser(head); }
Background image of page 12
Traverser?
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/23/2012 for the course CS 1371 taught by Professor Stallworth during the Fall '08 term at Georgia Institute of Technology.

Page1 / 42

CS2_28_UsefulTools - CS2 Module 28 Category: Elements of...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online