Data Structures Topic 12 ADTS, Data Structures, Java Collections S S C � A Data Structure is: and Generic Data Structures – an implementation of an abstract data type and – "An organization of information, usually in "Get your data structures correct computer memory", for better algorithm fi first, and the rest of the program will t d th t f th ill efficiency." ffi i " List Object write itself." aList aList 5 size - David Jones myElements y 0 1 2 3 4 5 6 7 8 9 10 A C E A C E B A B A CS 307 Fundamentals of ADTS and Generic Data Structures CS 307 Fundamentals of 1 2 Computer Science Computer Science ADTs and Data Structures Data Structure Concepts Core Operations � D � Data Structures are containers: � Data Structures will have 3 core operations S i – they hold other data – a way to add things – arrays are a data structure arra s are a data str ct re – a way to remove things – ... so are lists – a way to access things � Other types of data structures: � Other types of data structures: � Details of these operations depend on the – stack, queue, tree, data structure binary search tree, hash table, y , , dictionary or map, set, and on and on – Example: List, add at the end, access by – www.nist.gov/dads/ location, remove by location y – en.wikipedia.org/wiki/List_of_data_structures � More operations added depending on what � Different types of data structures are optimized for data structure is designed to do data structure is designed to do certain types of operations certain types of operations CS 307 Fundamentals of 3 CS 307 Fundamentals of 4 Computer Science ADTs and Data Structures Computer Science ADTs and Data Structures
ADTs and Data Structures in Data Structures in Java Programming Languages Programming Languages � Part of the Java Standard Library is the � Modern programming languages usually Collections Framework h have a library of data structures lib f d t t t – In class we will create our own data structures and discuss the data structures that exist in Java – Java collections framework � A library of data structures – C++ standard template library – .Net framework (small portion of VERY large � Built on two interfaces library) – Collection – Python lists and tuples – Iterator Iterator – Lisp lists � http://java.sun.com/j2se/1.5.0/docs/guide/coll ections/index html ections/index.html CS 307 Fundamentals of CS 307 Fundamentals of 5 6 Computer Science ADTs and Data Structures Computer Science ADTs and Data Structures The Java Collection interface Methods in the Collection interface public interface Collection<E> public interface Collection<E> � A generic collection { public boolean add (E o) public boolean addAll (Collection<? extends E> c) � Can hold any object data type public void clear () public void clear () � Which type a particular collection will hold is public boolean contains (Object o) public boolean containsAll (Collection<?> c) specified when declaring an instance of a spec ed e dec a g a sta ce o a public boolean equals (Object o) public boolean equals (Object o) public int hashCode () class that implements the Collection interface public boolean isEmpty () � Helps guarantee type safety at compile time Helps guarantee type safety at compile time 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 ea (Co ect o c) eta public int size () public Object[] toArray () p public <T> T[] toArray (T[] a) [] y ( [] ) } CS 307 Fundamentals of 7 CS 307 Fundamentals of 8 Computer Science ADTs and Data Structures Computer Science ADTs and Data Structures
ArrayList's (Partial) The Java ArrayList Class Class Diagram Class Diagram � Implements the List interface and uses an Iterable array as its internal storage container � It is a list, not an array Object Collection � The array that actual stores the elements of e a ay t at actua sto es t e e e e ts o the list is hidden, not visible outside of the AbstractCollection List ArrayList class ay s c ass � all actions on ArrayList objects are via the AbstractList methods methods � ArrayLists are generic. ArrayList – They can hold objects of any type! Th h ld bj t f t ! CS 307 Fundamentals of CS 307 Fundamentals of 9 10 Computer Science ADTs and Data Structures Computer Science ADTs and Data Structures Back to our Array Based List Using Object � Started with a list of ints � In Java, all classes inherit from exactly one other class except Object which is at the top � Don't want to have to write a new list class of the class hierarchy for every data type we want to store in lists � Object variables can point at objects of their � Moved to an array of Object s to store the y j declared type and any descendants elements of the list – polymorphism p y p // from array based list y private Object[] myCon; � Thus, if the internal storage container is of type Object it can hold anything type Object it can hold anything – primitives handled by wrapping them in objects. int – Integer, char - Character g , CS 307 Fundamentals of ADTS and Generic Data Structures 11 CS 307 Fundamentals of ADTS and Generic Data Structures 12 Computer Science Computer Science
Difficulties with Object Attendance Question 1 � Creating generic containers using the Object � What is output by the following code? data type and polymorphism is relatively ArrayList list = new ArrayList(); String name = "Olivia"; straight forward list.add(name); � Using these generic containers leads to System.out.print( list.get(0).charAt(2) ); System out print( list get(0) charAt(2) ); some difficulties A. i – Casting B. O – Type checking C. l � Code examples on the following slides Code examples on the following slides D. No output due to syntax error. E No output due to runtime error E. No output due to runtime error. CS 307 Fundamentals of ADTS and Generic Data Structures CS 307 Fundamentals of ADTS and Generic Data Structures 13 14 Computer Science Computer Science Code Example - Casting Code Example – type checking � Assume a list class //pre: all elements of li are Strings ArrayList li = new ArrayList(); public void printFirstChar(ArrayList li){ li.add(“Hi”); String temp; System.out.println( li.get(0).charAt(0) ); for(int i = 0; i < li.size(); i++) // previous line has syntax error // previous line has syntax error { temp = (String)li.get(i); // return type of get is Object if( temp.length() > 0 ) // Object does not have a charAt method // j S System.out.println( t t i tl ( // compiler relies on declared type temp.charAt(0) ); System.out.println( } ((String)li.get(0)).charAt(0) ); } // must cast to a String // what happens if pre condition not met? CS 307 Fundamentals of ADTS and Generic Data Structures 15 CS 307 Fundamentals of ADTS and Generic Data Structures 16 Computer Science Computer Science
Too Generic? Is this a bug or a feature? � Does the compiler allow this? ArrayList list = new ArrayList(); list.add( "Olivia" ); list.add( new Integer(12) ); list add( new Rectangle() ); list.add( new Rectangle() ); list.add( new ArrayList() ); A Yes A. Yes B. No CS 307 Fundamentals of ADTS and Generic Data Structures CS 307 Fundamentals of ADTS and Generic Data Structures 17 18 Computer Science Computer Science "Fixing" the Method Generic Types //pre: all elements of li are Strings // ll l t f li St i � Java has syntax for public void printFirstChar(ArrayList li){ parameterized data types String temp; String temp; � Referred to as Generic Types in most of the for(int i = 0; i < li.size(); i++){ literature if( li.get(i) instanceof String ){ ( g ( ) g ){ � A traditional parameter has a data type and temp = (String)li.get(i); if( temp.length() > 0 ) ca can store various values just like a variable s o e a ous a ues jus e a a ab e System.out.println( public void foo(int x) temp.charAt(0) ); � Generic Types are like parameters, but the � Generic Types are like parameters but the } data type for the parameter is data type } } } – like a variable that stores a data type lik i bl th t t d t t CS 307 Fundamentals of ADTS and Generic Data Structures 19 CS 307 Fundamentals of ADTS and Generic Data Structures 20 Computer Science Computer Science
Recommend
More recommend