Topic 12: "Get Your Data Structures Correct First, and The Rest of The Program Will Write Itself." - David Jones
Topic 12: "Get Your Data Structures Correct First, and The Rest of The Program Will Write Itself." - David Jones
"Get your data structures correct first, and the rest of the program will write itself." - David Jones
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
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." List Object aList 5 size myElements 0 1 2 3 4 5 6 7 8 9 10
3
A C E B A
CS 307 Fundamentals of Computer Science ADTs and Data Structures
Different types of data structures are optimized for certain types of operations
CS 307 Fundamentals of Computer Science ADTs and Data Structures
Core Operations
Data Structures will have 3 core operations
a way to add things a way to remove things a way to access things
http://java.sun.com/j2se/1.5.0/docs/guide/coll ections/index.html
CS 307 Fundamentals of Computer Science ADTs and Data Structures
10
Collection
List
11
12
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
13
14
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
15
16
17
Too Generic?
Does the compiler allow this?
ArrayList list.add( list.add( list.add( list.add( list = new ArrayList(); "Olivia" ); new Integer(12) ); new Rectangle() ); new ArrayList() );
A. Yes B. No
18
19
20
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
21
The value E stores will be filled in whenever a programmer creates a new GenericList
GenericList<String> li = new GenericList<String>();
CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures
22
Modifications to GenericList
instance variable
private E[] myCon;
Parameters on
add, insert, remove, insertAll
Return type on
get
23
24
list2.add( "Isabelle" ); System.out.println( list2.get(0).charAt(2) ); //ok list2.add( new Rectangle() ); // syntax error
25
New version
//pre: none
public void printFirstChar(ArrayList<String> li){
Elsewhere
ArrayList<String> list3 = new ArrayList<String>(); printFirstChar( list3 ); // ok ArrayList<Integer> list4 = new ArrayList<Integer>(); printFirstChar( list4 ); // syntax error
CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures
26
27