Sie sind auf Seite 1von 3

Example

Standard Inner Classes public class FixedStack {


Object [] array;
int top = 0;
• Defined like a class method/variable
class MyEnum implements java.util.Enumerator {
• Each instance associated with an instance of the int count = top;
outer class public boolean hasMoreElements() { return count > 0; }
• If class A is outer class public Object nextElement() {
if (count == 0)
– use A.this to get this for instance of outer class throw new NoSuchElementException(“FixedStack”);
• Can refer to all methods/variables of outer class return array[--count]; }
– transparently }
public java.util.Enumerator enumerateAll() {
• Can’t have any static methods/variables return new MyEnum(); }
}
CMCS 433, Spring 2002 - Adam Porter 1 CMCS 433, Spring 2002 - Adam Porter 2

Method class Example


Method and Anonymous Classes public class FixedStack {
Object [] array;
int top = 0;
• Can refer to all methods/variables of outer class public java.util.Enumerator enumerateOldestK(final int k) {
• Can refer to final local variables class MyEnum implements java.util.Enumerator {
int pos = 0;
• Can’t have any static methods/variables
public boolean hasMoreElements() {
• Method classes defined like a method variable return pos < k && pos < top; }
• Anonymous classes defined in new expression public Object nextElement() {
if (!hasMoreElements()) {
– new BaseClassOrInterface() { extensions }
throw new NoSuchElementException(“FixedStack”);
return array[pos++]; }
}
return new MyEnum(); }
CMCS 433, Spring 2002 - Adam Porter 3 } CMCS 433, Spring 2002 - Adam Porter 4

Anonymous class Example


Important details
public class FixedStack {
Object [] array;
int top = 0; • If class B is defined inside of class A
public java.util.Enumerator enumerateOldestK(final int k) { – a synchronized method of B locks B.this, not A.this
return new java.util.Enumerator() { – may want to lock A.this for synchronization
int pos = 0; – can have many B’s for each A
public boolean hasMoreElements()
• Can’t define constructor for anonymous inner
{ return pos < k && pos < top; }
public Object nextElement() {
class
if (!hasMoreElements()) • Inner classes are a compile-time transformation
throw new NoSuchElementException(“FixedStack”); – separate class file generated for each inner class
return array[pos++]; } – have $’s in names
}
}} CMCS 433, Spring 2002 - Adam Porter 5 CMCS 433, Spring 2002 - Adam Porter 6

CMSC 433, Adam Porter, U. Maryland (via Bill Pugh) 1


I/O Classes
• File
I/O and Utility Libraries – directories
• if(f.isDirectory()) System.out.println(f.list());
– interface FilenameFilter – allows selection of sublist
• OutputStream – byte stream going out
• Writer – character stream going out
• InputStream – byte stream coming in
• Reader – character stream coming in

CMCS 433, Spring 2002 - Adam Porter 8

Writer - characters
OutputStream - bytes • OutputStreamWriter
– wraps around OutputStream to get a Writer
• base types – takes characters, converts to bytes
– ByteArrayOutputStream – can specify encoding used to convert
– FileOutputStream – goes to file • CharArrayWriter
– PipedOutputStream – goes to PipedInputStream • StringWriter
• Filters – wrapped around an OutputStream • Filters
– BufferedOutputStream – PrintWriter – supports print, println
– ObjectOutputStream (for serializable objects) – BufferedWriter
• Convenience writers
– wrap OutputStreamWriter around an OutputStream
– FileWriter and PipedWriter
CMCS 433, Spring 2002 - Adam Porter 9 CMCS 433, Spring 2002 - Adam Porter 10

Reader - characters
InputStream - bytes • InputStreamReader
– wrap around InputStream to get a Reader
• base types – takes bytes, converts to characters
– ByteArrayInputStream – can specify encoding used to convert
– FileInputStream
• CharArrayReader
– PipedInputStream
– SocketInputStream (not public) – comes from TCP
• StringReader
socket • Filters
• Filters – wrapped around InputStream – BufferedReader – efficient, supports readLine()
– BufferedInputStream • LineNumberReader – reports line numbers

– PushedBackInputStream – PushBackReader
– ObjectInputStream • Convenience Readers
– wrap InputStreamReader around InputStream
– FileReader and PipedReader
CMCS 433, Spring 2002 - Adam Porter 11 CMCS 433, Spring 2002 - Adam Porter 12

CMSC 433, Adam Porter, U. Maryland (via Bill Pugh) 2


java.util Vector

• Vector • A list/vector abstraction


• Dictionaries • Can insert/delete/modify elements
• Enumerations and Bitsets anywhere in the list
• Collection classes • Can access by position
• Stack
– extension of Vector
– adds push, pop, peek and empty

CMCS 433, Spring 2002 - Adam Porter 13 CMCS 433, Spring 2002 - Adam Porter 14

Dictionaries Enumerations and Bitsets


• Dictionary • Enumeration
– an abstract class – an interface
– represents key to value mapping – used in many places to return an enumeration
• HashTable • public boolean hasMoreElements()
– an implementation of Dictionary • public Object nextElement()
– grows as needed • BitSet
– can be saved to a file (serializable, implements – provides representation of a set as a bit vector
deep toString)
– grows as needed (like HashTable)
CMCS 433, Spring 2002 - Adam Porter 15 CMCS 433, Spring 2002 - Adam Porter 16

Collection Classes
Other libraries
• interface Collection
– interface List • java.lang.Math
• class Vector (and Stack)
• class ArrayList – abstract final class – only static members
• class LinkedList – doubly linked – includes constants e and π
– interface Set – includes static methods for trig, exponentiation,
• class HashSet
min, max, …
• interface SortedSet
– class TreeSet • java.text
• interface Map – dictionary-like structures – text formatting tools
– class HashMap – replaces HashTable • class MessageFormat provides printf/scanf
– interface SortedMap functionality
• class TreeMap – lots of facilities for internationalization
CMCS 433, Spring 2002 - Adam Porter 17 CMCS 433, Spring 2002 - Adam Porter 18

CMSC 433, Adam Porter, U. Maryland (via Bill Pugh) 3

Das könnte Ihnen auch gefallen