Download Lecture 33: Iterators and String Buffer

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 33:
Iterators and String
Buffer
Last time:
1.
Midterm 2 Review 4/18
Today
1.
Iterators
2.
String Buffer
3.
Introducing inheritance …
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
Iterators
z
z
Special objects for iterating through elements in collection
Created using iterator() method of collection
ArrayList<String> a = …;
Iterator<String> i = a.iterator();
z
Iterator objects implement Iterator<T> interface
z Methods of interest
z
z
z
z
z
<T> next()
boolean hasNext()
void remove()
Return “current” element, advance
Is there a next element?
Remove current element
Be sure to import java.util.Iterator!
In array paradigm:
z next() equivalent to a[i++];
z hasNext() equivalent to i < a.length
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
1
Example
public static String concat (ArrayList<String> a){
Iterator<String> i = a.iterator();
String conc = “”;
while (i.hasNext()) {
conc += i.next();
}
return conc;
}
z
z
Concatenates elements in an ArrayList
Note use of iterator!
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
2
Iterator Behavior
z
z
When iterator is created, its “marker” starts at
initial element of collection
An iterator only moves forward
z
z
z
z
No backing up
No resetting
To cycle through data again, create another
iterator
See IteratorExample.java
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
3
Iterators and Mutability
z
z
z
z
If you are iterating through a collection and
someone adds or removes an element, your iterator
is no longer valid!!!
Usually a ConcurrentModification exception is
thrown
Calling the iterator's own remove() method does
not ruin the iterator
More than one iterator possible for the same
collection simultaneously
z
z
Do not use remove in this case!
Why? Because of ConcurrentModification problem
with other iterator
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
4
Mutable Strings
z
Strings are immutable
z
z
z
Sometime mutable strings would be handy
z
z
z
Once a String object is created, it cannot be altered
For String objects, reference = shallow = deep copying
(why?)
Sometimes a small change needs to be made to a string
(e.g. misspelled name)
Don’t want to create a whole new String object in this
case
StringBuffer: Java’s class for mutable Strings
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
5
StringBuffer Basics
z
z
See documentation at:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Stri
ngBuffer.html
Main methods
z
z
z
z
Note
z
z
z
append: add characters to end
insert: add characters in middle
delete: remove characters
append, insert return object of type StringBuffer
This is alias to object that the methods belong to!
See StringBufferExample.java
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
6
Inheritance
z
z
A crucial feature of object-oriented programming
languages
z One class (derived class, subclass) is constructed …
z … by importing (inheriting) information …
z … from another (base class, superclass, parent class) …
z … and adding new information / redefining existing
Example
z Base class: Clock
z
z
z
z
setTime
getTime
tick
Derived class: Alarm Clock
z
Same methods as Clock plus a few additional ones: setAlarm, ring
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
7
Can We Avoid Code Copying?
z
z
z
Clock “is a” Alarm Clock
Operations on Clock (e.g. setTime) should
be inherited by Alarm Clock
Alarm Clock should only have to add
information specific to alarm clocks
z
z
z
setAlarm
ring
Inheritance provides just this capability
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
8
Clock, Alarm Clock Example
z
Clock Class
class Clock {
private int hours, minutes, seconds;
public String getTime() {
// returns "HH:MM:SS"
...
}
public void setTime(int, int, int) {
...
}
public void tick() {
// adds one second
}
}
z
Alarm Clock Class
class AlarmClock extends Clock {
private int alarmHour, alarmMin, alarmSec;
public void setAlarm(int, int, int) {
...
}
public ring() {
...
}
}
CMSC 131 Spring 2007
Bonnie Dorr (Adapted from Rance Cleaveland)
9