Lecture 31 Collections Last time 1 Exceptions Today 1 Project 6 due 2 Midterm 11 15 3 Collections in Java 11 10 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 6 Assigned Project due Sunday 11 12 at 11 pm Project is closed You must complete the project by yourself Assistance can only be provided by teaching assistants TAs and instructors You must not look at other students code Start now Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly as much of grading is automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Midterm Exam Wed 11 15 Test will be given in discussion section Go to your own section Test will be Closed notes book neighbor etc Cover all material since beginning of course with special emphasis on topics since last midterm Study Review notes projects quizzes Use study questions on web site CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Exception Handling Example DateReader java Program uses Prompts user for a date in mm dd yyyy format Prints year substring method May throw IndexOutOfBoundsException Integer parseInt method May throw NumberFormatException How do we know about these exceptions Javadoc http java sun com j2se 1 5 0 docs api java lang pac kage summary html CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Javadoc Documentation Standard When documenting a method list exceptions that method can throw Use exception tag Be sure to include unhandled exceptions that operations in method may throw Example Returns the year part of a date string param d date string in mm dd yyyy format return an integer representing the date exception IndexOutOfBoundsException exception NumberFormatException public static int getYear String d CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Collections in Java Arrays are collections Arrays are objects Arrays are sequences of elements in base type These elements are collected together in one object the array Java includes may other collection mechanisms Arrays good for some applications fixed length sequences not others varying length sequences Other collections tuned for different purposes General observation holds however Collections are objects that contain other objects in a given type We ll study two more in 132 Stack ArrayList CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Stacks in Java Recall a stack is a data structure device for holding values Three operations on a stack push add a new value into the stack pop remove the most recently added value still in stack top return the most recently added value in stack Think stack of plates in a restaurant push put new plate on top pop remove top plate top look at top plate CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Example S push 3 S push 4 S top 4 S pop S push 5 S top 5 5 4 3 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland S 7 Stacks in Java cont Java includes a generic class for stack objects Stack objects contain other objects All objects in stack must have same type Only objects may be stored in stacks no primitive type values Notation Stack E Stack E is a generic class E is a class variable representing the base type Replace E by a specific type to get a stack of that type of elements Class is in java util package Example Stack String s new Stack String Creates a stack of strings Documentation http java sun com j2se 1 5 0 docs api java util Stack html See example ArrayExample java CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 ArrayList Collection Like arrays Sequences of elements All elements must be in same base type but support for inserting deleting new elements Syntax ArrayList E Documentation http java sun com j2se 1 5 0 docs api java util ArrayList html Example ArrayListExample java Collections sort may be used on ArrayList String objects Reason String implements Comparable interface ArrayList E implements List E interface CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Iterators What do you do with collections Access individual elements Add remove replace elements Process all elements in a collection Iterators are special objects supporting processing all elements in collections CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Iteration and Arrays Programming idiom coding scheme for processing elements in array a for int i 0 i a length i a i E g summing elements in a int sum 0 for int i 0 i a length i sum a i Idiom works because Arrays are ordered not sorted but ordered a 0 a 1 So loop counter can record which element is currently being accessed Accessing current element is possible via loop counter a i Last element is known a a length 1 so you know when all elements have been processed CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Other Kinds of Collections ArrayLists are also ordered Suppose we have ArrayList String a Similar programming idiom works ArrayList String a String concat for int i 0 i a size i concat a get i What about unordered Collections Not every collection has get int i method CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12
View Full Document