CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 1 Topic 12 ADTS, Data Structures, Java Collections and Generic Data Structures.

Slides:



Advertisements
Similar presentations
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Advertisements

AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
Java Generics.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
Topic 6 Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
12-Jul-15 Lists in Java Part of the Collections Framework.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Java's Collection Framework
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Arrays And ArrayLists - S. Kelly-Bootle
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Topic 3 The Stack ADT.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
CS 307 Fundamentals of Computer ScienceInterfaces and Abstract Classes 1 Topic 7 Interfaces and Abstract Classes “I prefer Agassiz in the abstract, rather.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
ArrayList, Multidimensional Arrays
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Chapter 18 Java Collections Framework
1/20/03A2-1 CS494 Interfaces and Collection in Java.
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Practical Session 4 Java Collections. Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones EE 422CGenerics 1.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CSE 1201 Object Oriented Programming ArrayList 1.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Collections Dwight Deugo Nesa Matic
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Sixth Lecture ArrayList Abstract Class and Interface
Topic 6 Generic Data Structures
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
JAVA Collections Framework Set Interfaces & Implementations
Polymorphism.
Dynamic Data Structures and Generics
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
ArrayLists 22-Feb-19.
Collections Framework
Dynamic Data Structures and Generics
Introduction to Data Structure
structures and their relationships." - Linus Torvalds
Polymorphism.
Presentation transcript:

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 1 Topic 12 ADTS, Data Structures, Java Collections and Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones

Abstract Data Types  Abstract Data Types (aka ADTs) are descriptions of how a data type will work without implementation details  Example, Stack at NIST DADS –  Description can be a formal, mathematical description  Java interfaces are a form of ADTs –some implementation details start to creep in CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 2

CS 307 Fundamentals of Computer ScienceADTs and Data Structures 3 Data Structures  A Data Structure is: –an implementation of an abstract data type and –"An organization of information, usually in computer memory", for better algorithm efficiency." aList List Object size myElements 5 A C E B A

CS 307 Fundamentals of Computer ScienceADTs and Data Structures 4 Data Structure Concepts  Data Structures are containers: –they hold other data –arrays are a data structure –... so are lists  Other types of data structures: –stack, queue, tree, binary search tree, hash table, dictionary or map, set, and on and on – –en.wikipedia.org/wiki/List_of_data_structuresen.wikipedia.org/wiki/List_of_data_structures  Different types of data structures are optimized for certain types of operations

CS 307 Fundamentals of Computer ScienceADTs and Data Structures 5 Core 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 location  More operations added depending on what data structure is designed to do

CS 307 Fundamentals of Computer ScienceADTs and Data Structures 6 ADTs and Data Structures in Programming Languages  Modern programming languages usually have a library of data structures –Java collections frameworkJava collections framework –C++ standard template libraryC++ standard template library –.Net framework (small portion of VERY large library).Net framework –Python lists and tuples –Lisp lists

CS 307 Fundamentals of Computer ScienceADTs and Data Structures 7 Data 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 –Iterator  ections/index.html ections/index.html

CS 307 Fundamentals of Computer ScienceADTs and Data Structures 8 The 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 class that implements the Collection interface  Helps guarantee type safety at compile time

CS 307 Fundamentals of Computer ScienceADTs and Data Structures 9 Methods in the Collection interface public interface Collection {public boolean add(E o) public boolean addAll(Collection c) public void clear() public boolean contains(Object o) public boolean containsAll(Collection c) public boolean equals(Object o) public int hashCode() public boolean isEmpty() public Iterator iterator() public boolean remove(Object o) public boolean removeAll(Collection c) public boolean retainAll(Collection c) public int size() public Object[] toArray() public T[] toArray(T[] a) }

CS 307 Fundamentals of Computer ScienceADTs and Data Structures 10 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 the list is hidden, not visible outside of the ArrayList class  all actions on ArrayList objects are via the methods  ArrayLists are generic. –They can hold objects of any type!

CS 307 Fundamentals of Computer ScienceADTs and Data Structures 11 ArrayList's (Partial) Class Diagram AbstractCollection Object AbstractList ArrayList Iterable Collection List

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 12 Back 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 Object s to store the elements of the list // from array based list private Object[] myCon;

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 13 Using 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 –polymorphism  Thus, if the internal storage container is of type Object it can hold anything –primitives handled by wrapping them in objects. int – Integer, char - Character

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 14 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 –Casting –Type checking  Code examples on the following slides

Attendance 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) ); A. i B. O C. l D. No output due to syntax error. E. No output due to runtime error. CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 15

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 16 Code Example - Casting  Assume a list class ArrayList li = new ArrayList(); li.add(“Hi”); System.out.println( li.get(0).charAt(0) ); // previous line has syntax error // return type of get is Object // Object does not have a charAt method // compiler relies on declared type System.out.println( ((String)li.get(0)).charAt(0) ); // must cast to a String

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 17 Code Example – type checking //pre: all elements of li are Strings public void printFirstChar(ArrayList li){ String temp; for(int i = 0; i < li.size(); i++) {temp = (String)li.get(i); if( temp.length() > 0 ) System.out.println( temp.charAt(0) ); } // what happens if pre condition not met?

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 18 Too Generic?  Does the compiler allow this? ArrayList list = new ArrayList(); list.add( "Olivia" ); list.add( new Integer(12) ); list.add( new Rectangle() ); list.add( new ArrayList() ); A.Yes B.No

Is this a bug or a feature? CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 19

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 20 "Fixing" the Method //pre: all elements of li are Strings public void printFirstChar(ArrayList li){ String temp; for(int i = 0; i < li.size(); i++){ if( li.get(i) instanceof String ){ temp = (String)li.get(i); if( temp.length() > 0 ) System.out.println( temp.charAt(0) ); }

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 21 Generic Types  Java has syntax for parameterized data types  Referred to as Generic Types in most of the literature  A traditional parameter has a data type and can store various values just like a variable public void foo(int x)  Generic Types are like parameters, but the data type for the parameter is data type –like a variable that stores a data type

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 22 Making our Array List Generic  Data type variables declared in class header public class GenericList {  The is the declaration of a data type parameter for the class –any legal identifier: Foo, AnyType, Element, DataTypeThisListStores –Sun style guide recommends terse identifiers  The value E stores will be filled in whenever a programmer creates a new GenericList GenericList li = new GenericList ();

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 23 Modifications to GenericList  instance variable private E[] myCon;  Parameters on –add, insert, remove, insertAll  Return type on –get  Changes to creation of internal storage container myCon = (E[])new Object[DEFAULT_SIZE];  Constructor header does not change

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 24 Using Generic Types  Back to Java's ArrayList ArrayList list1 = new ArrayList(); –still allowed, a "raw" ArrayList –works just like our first pass at GenericList –casting, lack of type safety

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 25 Using Generic Types ArrayList list2 = new ArrayList (); –for list2 E stores String list2.add( "Isabelle" ); System.out.println( list2.get(0).charAt(2) ); //ok list2.add( new Rectangle() ); // syntax error

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 26 Parameters and Generic Types  Old version //pre: all elements of li are Strings public void printFirstChar(ArrayList li){  New version //pre: none public void printFirstChar(ArrayList li){  Elsewhere ArrayList list3 = new ArrayList (); printFirstChar( list3 ); // ok ArrayList list4 = new ArrayList (); printFirstChar( list4 ); // syntax error

CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 27 Generic Types and Subclasses ArrayList list5 = new ArrayList (); list5.add( new Rectangle() ); list5.add( new Square() ); list5.add( new Circle() ); // all okay  list5 can store ClosedShape s and any descendants of ClosedShape