DOC PREVIEW
UMD CMSC 131 - Lecture 26: Collections

This preview shows page 1 out of 2 pages.

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

Unformatted text preview:

1CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)Lecture 26:CollectionsLast time:1.ExceptionsToday1.Collections : StackCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)1Collections 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 CMSC132): Stack, ArrayListCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)2Stacks in JavaRecall: a stack is a data structure (“device”for holding values) – FILO (First In, Last Out)Typical 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 stackNote: Java calls this “peek”is empty: returns true if the stack is currently empty or false otherwise2CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)3Example of stack concept (not Java specific)Stack s s.isempty() == ??true s.push (3);s.isempty() == ??false s.push (4);s.peek == ??4s.pop ();s.push (5);s.peek == ??5s345CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)4Stacks 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)Syntax: 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Documentation: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.htmlSee 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


View Full Document

UMD CMSC 131 - Lecture 26: 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 26: 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 26: 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 26: 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?