Contents [0/6] |
Abstract Data Type (ADT) [1/6] |
ADTs in Java [2/6] |
Data structure [3/6] |
Generic classes [4/6] |
Interfaces [5/6] |
Summary: Data structures in Java [6/6] |
(Click here for one slide per page)
Abstract Data Type (ADT) [1/6] |
An Abstract Data Type (ADT) specifies
For example, a stack (think of a stack of plates) includes the operations to :
s
ADTs in Java [2/6] |
In Java we write this as follows (using Strings instead of plates):
01 |
package algs13; public class StackOfStrings { public StackOfStrings { /* ... */ } public boolean isEmpty () { /* ... */ } public void push (String item) { /* ... */ } public String pop () { /* ... */ } } |
We use the ADT as follows:
08 |
StackOfStrings stack = new StackOfStrings(); stack.push("a"); if (! stack.isEmpty()) { String item = stack.pop(); } |
Note that I've told you what the operations are, and how they should behave.
I've told you nothing about how the operations are implemented.
When we look at a specific implementation, this is what we call a data structure.
Data structure [3/6] |
A data structure provides code to implement an ADT.
The most important choice is how to store data.
For stack, we will look at two choices:
The way we choose to store the data affects the performance of the operations!
Generic classes [4/6] |
A generic class has a type parameter. Type is not stored at runtime.
file:Stack.java [source]
01
02
03
04
05
06
07
08
09
10
package ds1.student.stack; /// An interface defining the Stack ADT. /// DO NOT CHANGE THIS INTERFACE. public interface Stack<T> extends Iterable<T> { void push(T t); T pop(); boolean isEmpty(); int size(); }
Interfaces [5/6] |
Some language features use special interfaces, such as Iterable
.
An Iterator
is a design pattern.
Iterator
allows you go through the elements of a data structure without imposing on the data sturcture itself.
Iterable
is an interface that gives you access to an Iterator
.
Summary: Data structures in Java [6/6] |
Abstract data types are implemented as data structures.
Data structures are realized in Java using classes.
We hold the data associated with a data structure using fields (aka member variables or instance variables).
Fields are different from local variables, which are stored in methods.