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 StructuresA 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 ConceptsDS iData Structures are containers:– they hold other dataarra s are a data str ct re–arrays are a data structure– ... so are listsOther 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_structuresDifferent types of data structures are optimized for certain types of operationsCS 307 Fundamentals of Computer Science ADTs and Data Structures3certain types of operationsCore OperationsData Structures will have 3 core operations– a way to add things– a way to remove things– a way to access thingsDetails of these operations depend on the data structure– Example: List, add at the end, access by location, remove by locationyMore 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 LanguagesModern 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 JavaPart 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 JavaA library of data structuresBuilt on two interfaces– Collection–IteratorIteratorhttp://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 interfaceA generic collectionCan hold any object data typeWhich 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 interfaceHelps 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 ClassImplements the List interface and uses an array as its internal storage containerIt is a list, not an arrayThe 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 assall actions on ArrayList objects are via the methodsmethodsArrayLists 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 intsDon't want to have to write a new list class for every data type we want to store in listsMoved 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 ObjectIn Java, all classes inherit from exactly one other class except Object which is at the top of the class hierarchyObject variables can point at objects of their declared type and any descendants–polymorphismpy pThus, 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 ObjectCreating generic containers using the Object data type and polymorphism is relatively straight forwardUsing these generic containers leads to some difficulties–Castingg– Type checkingCode examples on the following slidesCode examples on the following slidesCS 307 Fundamentals of Computer ScienceADTS and Generic Data Structures13Attendance Question 1What 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 - CastingAssume 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