0% found this document useful (0 votes)
2 views

Topic12ADTS GenericDataStructures

generic data structures

Uploaded by

MAJID
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Topic12ADTS GenericDataStructures

generic data structures

Uploaded by

MAJID
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

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

CS 307 Fundamentals of ADTS and Generic Data Structures 1


Computer Science
Abstract Data Types
 Abstract Data Types (aka ADTs) are
descriptions of how a data type will work
without implementation details
 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 ADTS and Generic Data Structures 2


Computer Science
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
size 5
myElements
0 1 2 3 4 5 6 7 8 9 10
A C E B A
CS 307 Fundamentals of 3
Computer Science ADTs and Data Structures
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
 Different types of data structures are optimized for
certain types of operations

CS 307 Fundamentals of 4
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
 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 5
Computer Science ADTs and Data Structures
ADTs and Data Structures in
Programming Languages
 Modern programming languages usually
have 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 lists

CS 307 Fundamentals of 6
Computer Science ADTs and Data Structures
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
 http://java.sun.com/j2se/1.5.0/docs/guide/
collections/index.html
CS 307 Fundamentals of 7
Computer Science ADTs and Data Structures
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 8
Computer Science ADTs and Data Structures
Methods in the Collection interface
public interface Collection<E>
{ public boolean add(E o)
public boolean addAll(Collection<? extends E> 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<E> iterator()
public boolean remove(Object o)
public boolean removeAll(Collection<?> c)
public boolean retainAll(Collection<?> c)
public int size()
public Object[] toArray()
public <T> T[] toArray(T[] a)
}
CS 307 Fundamentals of 9
Computer Science ADTs and Data Structures
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 10
Computer Science ADTs and Data Structures
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 ADTS and Generic Data Structures 11
Computer Science
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 ADTS and Generic Data Structures 12


Computer Science
Generic Types and Subclasses
ArrayList<ClosedShape> list5 =
new ArrayList<ClosedShape>();
list5.add( new Rectangle() );
list5.add( new Square() );
list5.add( new Circle() );
// all okay
 list5 can store ClosedShapes and any
descendants of ClosedShape

CS 307 Fundamentals of ADTS and Generic Data Structures 13


Computer Science

You might also like