Docsity

Java Collections Framework LinkedList Implementation Work on Markov | CSSE 220, Study notes of Computer Science

Partial preview of the text

Download Java Collections Framework LinkedList Implementation Work on Markov | CSSE 220 and more Study notes Computer Science in PDF only on Docsity!

Java Collections FrameworkLinkedList ImplementationWork on Markov

^ Reminder: Exam #2 is (next) Friday, May 2.^ In order to reduce time pressure, youoptionally may take the non-programming part7:15-8:00 AM. ^ Markov repositories:^ ◦^ http://svn.cs.rose-hulman.edu/repos/220-200820-markovXXX^ Questions? `^ Today:^ ◦^ Java Collections^ ◦^ Iterators^ ◦^ Begin implementing lists

Java Collections http://www.falkhausen.com/en/diagram/html/java.util.Collection.html

`^ The main Java tool for specifying an ADT is …^ ◦^ … an interface^ ◦^ Major example: The

j ava.util.Collection interface.

`^ Some important methods from this interface:

Factory method

Iterators^ `^ Consider a loop to fund the sum of eachelement in an array:^ for^ (int^ i^ =^ 0;^

i^ <^ ar.length;^ i++)

{

sum^ +=^ ar[i];} We want to generalize this beyondarrays

Example: Using an Iterator^ ag^ is a Collection object.^ Using Java 1.5’s “foreach” construct: Note that the Java compiler essentially translates the latter code into the former.

What's an iterator? ` More specifically, what is a j

ava.util.Iterator

? ◦^ It's an interface: ◦^ interface^ java.util.Iterator ◦^ with the following methods: ` Why do iterators have their own remove method, separatefrom the Collections’ remove?

Sort and Binary Search ` The java.util.Arrays

class provides static methods for sorting and doing binary search on arrays.Examples:

ag can be any^ Collection

of^ Integers In Java 1.5 we can simplify it even more. Note that the Java compiler translates the latter code into the former.

^ addAll – add all of the elements from anothercollection to this one^ containsAll – does this collection contain all ofthe elements of the other collection? ^ removeAll – removes all of this collectionselements that are also contained in the othercollection^ retainAll - removes all of this collectionselements that are not contained in the othercollection `^ toArray – returns an array that contains the sameelements as this collection.

`^ The^ java.util.Arrays

class provides static methods for

sorting and doing binary search on arrays.

Examples:

^ In weiss.util, the author shows "bare bones"possible implementations of some of theclasses in java.util.^ He picks the methods that illustrate theessence of what is involved in theimplementation, for educational purposes. `^ Some other Data Structures classes are inweiss.nonstandard.

`^ In weiss.nonstandard, the author showsimplementations of some common datastructures that are not part of the java.utilpackage, and he also shows alternateapproaches to implementing some classes(like Stack and LinkedList) that are injava.util.