1CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)Lecture Set #18:Collections1.New Looping construct1.for each loop2.Collections1.Stack2.ArrayListCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)1Collections in JavaArrays are collectionsArrays are objectsArrays are sequences of elements in base typeThese elements are collected together in one object: the arrayJava includes may other collection mechanismsArrays good for some applications (fixed-length sequences), not others (varying-length sequences)Other collections tuned for different purposesGeneral observation holds, however:Collections are objects …… that contain other objects in a given typeWe’ll study two (more in CMSC132): Stack, ArrayList2CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)2for … each … inNew construct available in Java 1.5 (not available in older versions of Java)Use with arraysUse with any iteratable collectionLimitations:modifications limitedcan’t add items to the list being iterated overcan’t remove items from the list being iterated overcan’t replace items in the list being iterated overaccess only oneonly a single collection can be traversed at a timecan’t access the one before or the one after on this iterationlimited to forward and one at a timecan’t traverse the list in the reverse ordercan’t go to every other element or any variationCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)3Stacks in JavaRecall: a stack is a data structure (“device”for holding values) – FILO (First In, Last Out)Typical operations on a stackpush: add a new value into the stackpop: remove the most recently added value still in stacktop: return the most recently added value in stackNote: Java calls this “peek”is empty: returns true if the stack is currently empty or false otherwise3CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)4Example of stack concept (not Java specific)Stack s s.isempty() == ??true s.push (3);s.isempty() == ??false s.push (4);s.peek == ??4s.pop ();s.push (5);s.peek == ??5s345CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)5Stacks in Java (cont.)Java includes a generic class for stack objectsStack objects contain other objectsAll objects in stack must have same typeOnly objects may be stored in stacks (no primitive-type values)Syntax: Stack<E>Stack<E> is a generic classE is a class variable representing the base typeReplace E by a specific type to get a stack of that type of elementsClass is in java.util packageDocumentation: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.htmlSee example: StackExample.javaStack<String> stack = new Stack<String>();Creates a stack of strings-extend this to be stack of cats-extend this to be stack of integer values4CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)6ArrayList CollectionLike arrays … but support for inserting/deleting new elementsSequences of elementsAll elements must be in same (base) typeSyntax: ArrayList<E>Documentation: http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.htmlSee example: ArrayListExample.javaArrayList<String> a = new ArrayList<String>();Creates an ArrayList of stringsCollections.sort may be used on ArrayList<String> objects?ReasonString implements Comparable interfaceArrayList<E> implements List<E> interfaceCMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)7Mutable StringsStrings are immutableOnce a String object is created, it cannot be alteredFor String objects, reference = shallow = deep copying (why?)Sometime mutable strings would be handySometimes 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 caseStringBuffer: Java’s class for mutable Strings5CMSC 131 Spring 2008Jan Plane (adapted from Bonnie Dorr)8StringBuffer BasicsSee documentation at:http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.htmlMain methodsappend: add characters to endinsert: add characters in middledelete: remove charactersNoteappend, insert return object of type StringBufferThis is alias to object that the methods belong to!See
View Full Document