DOC PREVIEW
UT CS 307 - Topic 12- ADTs , Data structures, Java collections and Generic Data Structures

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 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 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Topic 12SS CADTS, Data Structures, Java Collections and Generic Data Structures"Get your data structures correct fi t d th t f th illfirst, and the rest of the program will write itself."- David JonesCS 307 Fundamentals of Computer ScienceADTS and Generic Data Structures1Data StructuresA Data Structure is:– an implementation of an abstract data type and– "An organization of information, usually in computer memory", for better algorithm ffi i "efficiency."aListList ObjectaListsizemyElements5yACEBA0 1 2 3 4 5 6 7 8 9 10CS 307 Fundamentals of Computer Science ADTs and Data Structures2A C E B AData Structure ConceptsDS iData Structures are containers:– they hold other dataarra s are a data str ct re–arrays are a data structure– ... so are listsOther types of data structures:Other types of data structures:– stack, queue, tree, binary search tree, hash table,y,,dictionary or map, set, and on and on– www.nist.gov/dads/–en.wikipedia.org/wiki/List_of_data_structuresDifferent types of data structures are optimized for certain types of operationsCS 307 Fundamentals of Computer Science ADTs and Data Structures3certain types of operationsCore OperationsData Structures will have 3 core operations– a way to add things– a way to remove things– a way to access thingsDetails of these operations depend on the data structure– Example: List, add at the end, access by location, remove by locationyMore operations added depending on what data structure is designed to doCS 307 Fundamentals of Computer Science ADTs and Data Structures4data structure is designed to doADTs and Data Structures in Programming LanguagesProgramming LanguagesModern programming languages usually h lib f d t t thave a library of data structures– Java collections framework– C++ standard template library– .Net framework (small portion of VERY large library)– Python lists and tuples– Lisp listsCS 307 Fundamentals of Computer Science ADTs and Data Structures5Data Structures in JavaPart of the Java Standard Library is the Collections Framework– In class we will create our own data structures and discuss the data structures that exist in JavaA library of data structuresBuilt on two interfaces– Collection–IteratorIteratorhttp://java.sun.com/j2se/1.5.0/docs/guide/collections/index htmlCS 307 Fundamentals of Computer Science ADTs and Data Structures6ections/index.htmlThe Java Collection interfaceA generic collectionCan hold any object data typeWhich type a particular collection will hold is specified when declaring an instance of a spec ed e dec a g a sta ce o aclass that implements the Collection interfaceHelps guaranteetype safetyat compile timeHelps guarantee type safetyat compile timeCS 307 Fundamentals of Computer Science ADTs and Data Structures7Methods in the Collection interfacepublic interface Collection<E>public interface Collection<E>{ public boolean add(E o)public boolean addAll(Collection<? extends E> c)public voidclear()public void clear() public boolean contains(Object o) public boolean containsAll(Collection<?> c) public booleanequals(Objecto)public boolean equals(Objecto)public int hashCode() public boolean isEmpty() public Iterator<E>iterator()public Iterator<E> iterator()public boolean remove(Object o) public boolean removeAll(Collection<?> c) public boolean retainAll(Collection<?>c)pub c boo eaeta(Co ect oc)public int size() public Object[] toArray()public <T> T[] toArray(T[]a) CS 307 Fundamentals of Computer Science ADTs and Data Structures8p[]y([])}The Java ArrayList ClassImplements the List interface and uses an array as its internal storage containerIt is a list, not an arrayThe array that actual stores the elements of e a ay t at actua sto es t e e e e ts othe list is hidden, not visible outside of the ArrayList classay s c assall actions on ArrayList objects are via the methodsmethodsArrayLists are generic. Th h ld bj t f t !CS 307 Fundamentals of Computer Science ADTs and Data Structures9–They can hold objects of any type!ArrayList's (Partial) Class DiagramClass DiagramIterableObjectCollectionAbstractCollectionListAbstractListArrayListCS 307 Fundamentals of Computer Science ADTs and Data Structures10Back to our Array Based List Started with a list of intsDon't want to have to write a new list class for every data type we want to store in listsMoved to an array of Objects to store the yjelements of the list// from array based listyprivate Object[] myCon;CS 307 Fundamentals of Computer ScienceADTS and Generic Data Structures11Using ObjectIn Java, all classes inherit from exactly one other class except Object which is at the top of the class hierarchyObject variables can point at objects of their declared type and any descendants–polymorphismpy pThus, if the internal storage container is of type Object it can hold anythingtype Object it can hold anything– primitives handled by wrapping them in objects.int –Integer, char - CharacterCS 307 Fundamentals of Computer ScienceADTS and Generic Data Structures12g,Difficulties with ObjectCreating generic containers using the Object data type and polymorphism is relatively straight forwardUsing these generic containers leads to some difficulties–Castingg– Type checkingCode examples on the following slidesCode examples on the following slidesCS 307 Fundamentals of Computer ScienceADTS and Generic Data Structures13Attendance Question 1What is output by the following code?ArrayList list = new ArrayList();String name = "Olivia";list.add(name);System out print(list get(0)charAt(2) );System.out.print( list.get(0).charAt(2) );A. iBB. OC. lD. No output due to syntax error.E No output due to runtime errorE. No output due to runtime error.CS 307 Fundamentals of Computer ScienceADTS and Generic Data Structures14Code Example - CastingAssume a list classArrayList li = new ArrayList();li.add(“Hi”);System.out.println( li.get(0).charAt(0) );// previous line has syntax error// previous line has syntax error// return type of get is Object// Object does not have a charAt method// j// compiler relies on declared typeSystem.out.println( ((String)li.get(0)).charAt(0) );// must cast to a StringCS 307 Fundamentals of Computer ScienceADTS and Generic Data Structures15Code Example – type checking//pre: all elements of li are Stringspublic void printFirstChar(ArrayList li){String


View Full Document

UT CS 307 - Topic 12- ADTs , Data structures, Java collections and Generic Data Structures

Documents in this Course
Midterm 2

Midterm 2

15 pages

Midterm 1

Midterm 1

15 pages

Syllabus

Syllabus

24 pages

s

s

8 pages

Midterm 1

Midterm 1

14 pages

Midterm 2

Midterm 2

14 pages

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download Topic 12- ADTs , Data structures, Java collections and Generic Data Structures
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 Topic 12- ADTs , Data structures, Java collections and Generic Data Structures 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 Topic 12- ADTs , Data structures, Java collections and Generic Data Structures 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?