CS3101-3 Programming Language-JAVA Fall 2004 Oct.13rd
CS3101-3 Programming Language - JAVA Fall 2004 Oct.13rd
Roadmap today ●Review ●Inner class ●Multi--threading ●Graphics
Roadmap today lReview lInner class lMulti-threading lGraphics
Containers O Hold a group of objects o Significantly increase your programming power OAll perform bound checking ● array:efficient,can hold primitives Collection:a group of individual elements OList,Set o Map:a group of key-value object pairs OHashMap o Misleading:sometimes the whole container libraries are also called collection classes
Containers l Hold a group of objects l Significantly increase your programming power l All perform bound checking l array: efficient, can hold primitives l Collection: a group of individual elements ¡List, Set l Map: a group of key-value object pairs ¡HashMap l Misleading: sometimes the whole container libraries are also called collection classes
Collection:hold one item at each location List:items in order Set:no duplicates,no ordering ArrayList List LinkedList Vector Collection Preserve the insertion of the elements Set HashSet LinkedHashSet TreeSet
Collection List ArrayList LinkedList Vector Set HashSet LinkedHashSet TreeSet Collection: hold one item at each location List: items in order Set: no duplicates, no ordering Preserve the insertion of the elements
Map:key-value pairs,fast retrieval no duplicate keys,no ordering HashMap LinkedHashMap Map Hashtable Preserve the insertion of the elements TreeMap
Map HashMap LinkedHashMap Hashtable TreeMap Map: key-value pairs, fast retrieval no duplicate keys, no ordering Preserve the insertion of the elements
Disadvantages of container o Cannot hold primitives OHave to wrap it o Lose type information when put object into container OEverything is just Object type once in container o Have to do cast when get it out OYou need to remember what's inside o Java do run time type check OClassCastException
Disadvantages of container lCannot hold primitives ¡Have to wrap it lLose type information when put object into container ¡Everything is just Object type once in container lHave to do cast when get it out ¡You need to remember what’s inside lJava do run time type check ¡ClassCastException
Iterator object OAccess method regardless of the underlying structure ●Generic programming OCan change underlying structure easily ●“light--weight”object OCheap to create o Can move in only one direction
Iterator object lAccess method regardless of the underlying structure lGeneric programming ¡Can change underlying structure easily l“light-weight” object ¡Cheap to create lCan move in only one direction
Iterator constraints oContainer.iterator()returns you an Iterator, which is ready to return the first element in the sequence on your first call to next() ●( Get the next object in the sequence with next() ● Set there are more objects in the sequence with hasNext() o Remove the last element returned by the iterator with remove() Example:revisit CatsAndDogs.java
Iterator constraints l Container.iterator() returns you an Iterator, which is ready to return the first element in the sequence on your first call to next() l Get the next object in the sequence with next() l Set there are more objects in the sequence with hasNext() l Remove the last element returned by the iterator with remove() l Example: revisit CatsAndDogs.java
Java l/O:lowest level abstraction InputStream/OutputStream class OByte-oriented Oread()return the byte got read Owrite(int b)writes one byte out oCan obtain from console,file,or socket o Handle them in essentially the same way oWrite your code to work with Streams and won't care if it's talking to a file or a system on the other side of the world
Java I/O: lowest level abstraction lInputStream/OutputStream class ¡Byte-oriented ¡read() return the byte got read ¡write(int b) writes one byte out lCan obtain from console, file, or socket lHandle them in essentially the same way lWrite your code to work with Streams and won’t care if it’s talking to a file or a system on the other side of the world
Serialization An important feature of Java o Convert an object into a stream of byte, and can later deserialize it into a copy of the original object o Takes care of reassembling objects oNeed to cast type when read back Any object as long as it implements Serializable interface ONo method inside
Serialization lAn important feature of Java lConvert an object into a stream of byte, and can later deserialize it into a copy of the original object lTakes care of reassembling objects lNeed to cast type when read back lAny object as long as it implements Serializable interface ¡No method inside