DOC PREVIEW
UMD CMSC 131 - Lecture 31: Collections

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11/10/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 31:CollectionsLast time:1. ExceptionsToday1. Project #6 due2. Midterm 11/153. Collections in JavaCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Project #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 automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Midterm 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-siteCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Exception Handling: Example DateReader.java Prompts user for a date in mm/dd/yyyy format Prints year Program uses: substring methodMay throw IndexOutOfBoundsException Integer.parseInt methodMay throw NumberFormatException How do we know about these exceptions? Javadoc!http://java.sun.com/j2se/1.5.0/docs/api/java/lang/package-summary.htmlCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Javadoc 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 2006Rance Cleaveland©2006 University of Maryland5Collections 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, ArrayListCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Stacks 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 plateCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7Example S.push (3); S.push (4); S.top == ??4 S.pop (); S.push (5); S.top == ??5S345CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Stacks 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 ExampleStack<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.javaCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9ArrayList Collection Like arrays … Sequences of elements All elements must be in same (base) type … but support for inserting / deleting new elements Syntax: ArrayList<E> Documentationhttp://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> interfaceCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Iterators 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 collectionsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Iteration 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 aint 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 processedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Other Kinds of Collections? ArrayLists are also ordered Suppose we haveArrayList<String> a = …; Similar programming idiom worksArrayList<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)


View Full Document

UMD CMSC 131 - Lecture 31: Collections

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 31: Collections
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 31: Collections and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 31: Collections 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?