dynamic data structures and generics chapter 10. outline vectors linked data structures introduction...

58
Dynamic Data Structures and Generics Chapter 10

Upload: austen-gilmore

Post on 05-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Dynamic Data Structures and Generics

Chapter 10

Page 2: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Outline

• Vectors• Linked Data Structures• Introduction to Generics

Page 3: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Introduction

• A data structure is a construct used to organize data in a specific way.

• An array is a static data structure.• Dynamic data structures can grow and shrink

while a program is running.• Vectors and linked data structures are

dynamic.• Vectors are similar to arrays, but offer more

flexibility.

Page 4: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Introduction, cont.

• The linked list is a simple but useful linked data structure that makes use of inner classes.

• An inner class is a class definition within another class definition.

• Java 5.0 allows definitions with parameters for types.

• These definitions are known as generics.

Page 5: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Vectors: Outline

• Introduction to Vectors• Using Vectors• Parameterized Classes and Generics

Page 6: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Introduction to Vectors

• We can think of vectors as arrays that grow and shrink while a program is running.

• At the time an array is created, its length is fixed.– Sometimes an array turns out to be too

small for its intended use.– Sometimes an array turns out to be too

large for its intended use, but the unused portion of the array is not available for other purposes.

Page 7: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Introduction to Vectors, cont.

• Vectors serve the same purposes as arrays, but can change in length while a program is running.

• This added flexibility comes at a price:– Vectors are less efficient than arrays.– The base type of a vector must be a class

type rather than a primitive type. (Automatic boxing and unboxing make this requirement less significant.)

Page 8: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Using Vectors

• The definition of class Vector must be imported.import java.util.*;

• to create and name a vectorvector<String> v = new Vector<String>(20);

– The vector v stores objects of class String and has an initial capacity of 20.

Page 9: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Using Vectors, cont.

– When more capacity is needed, the system allocates more memory automatically.

– If the initial capacity was sufficient, the code is more efficient.

• In this example, the base type is type String.– Any class can be used as the base type.– But, wrapper classes must be used for

primitive types.

Page 10: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Creating and Naming a Vector

• syntaxVector<Base_Type> v1 = new

Vector<Base_Type>();

Vector<Base_Type> v2 = new

Vector<Base_Type>(Initial_Capacity);

Vector<Base_Type> v2 = new

Vector<Base_Type>(Initial_Capacity,

Capacity_Increment);

Page 11: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Adding, Getting, and Setting Values

• to add an elementv.addElement(“Hello!);

• to get the value of an elementString temp = v.elementAt(index);

• to change the value of an existing elementv.setElementAt(“Hi, Mom!”, index);

Page 12: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Inserting and Removing Values

• to insert an elementv.insertElementAt(“Good-bye”, position);

– elements at index position or higher move to index positions greater by one.

• to remove an element from a positionv.removeElementAt(postion);

• to remove the first occurrence of an elementboolean done = v.removeElement(“Hello!”);

• to remove all elementsv.removeAllElements();

Page 13: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Searching a Vector

• to learn if an element is in the vectorboolean found = v.contains(“Good-bye”);

• to learn the location of the first occurrence of an elementint position = v.indexOf(“Hi, Mom!”);

• to learn the location of the first occurrence of an element at or after a positionint position = v.indexOf(“Hello”,

startFrom);

Page 14: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Searching a Vector, cont.

• to learn the location of the last occurrence of an elementint position =

v.lastIndexOf(“Hi, Mom!”);

• to learn the value of the first elementString first = v.firstElement();

• to learn the value of the last elementString first = v.lastElement();

Page 15: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Size and Capacity• to learn if the vector is empty

boolean none = v.isEmpty();

• to learn the size of the vectorint howMany = v.size();

• to learn the current capacityint howBig = v.capacity();

• to make room for more elementsv.ensureCapacity(moreElements);

• to trim to the current sizev.trimToSize();

• to set the sizev.setSize(howMany);

Page 16: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Vector Demonstration

Page 17: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Using Method clone

• When used with a vector, the assignment statement creates another reference to the same vector.

• To make a copy of a vector, useotherV = (Vector<String>)v.clone();

using a correct type cast.• On the other hand

Vector otherV = v.clone(); //ILLEGAL

will produce an error message.

Page 18: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Using Method clone, cont.

• Accessor methods should not return a private instance variable of type Vector.

• Accessor methods should return a copy of the vector, not the private instance vector itself.

• Method clone can be used to produce a copy of the private instance vector.

Page 19: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Using Method clone, cont.

• However, the return type of method clone is Object.

• Hence, the appropriate form ispublic Vector<String> getVector()

{

return (Vector<String>)v.clone();

}

• Any objects stored in the vector also must have appropriate accessor methods.

Page 20: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Parameterized Classes and Generics

• The class Vector is a parameterized class.• Its parameter, denoted Base_Type, can be

replaced by any class type.• Java 5.0 allows definitions, called generic

definitions or simply generics, with parameters for types.

Page 21: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

(optional) Newer Collection Classes

• A new group of classes implement the Collection interface.– These classes are known as collection

classes.• The Vector definition has been retrofitted to be

a collection class.

Page 22: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

AbstractList class

• Roughly equivalent to Vector except that it is unsynchronized.– More efficient in environments that

synchronization are not required

Page 23: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Linked Data Structures: Outline

• Linked Lists• Inner Classes• Node Inner Classes• Other Linked Data Structures

Page 24: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Introduction to Linked Data Structures

• A linked data structure is a collection of objects (called nodes), each containing data and a (potential) reference to (at least) one other node.

Page 25: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Linked Lists

• The predefined LinkedList class is part of the java.util package.

• Nevertheless, to learn how linked data structures work, we’ll construct a simplified example of a linked list.

Page 26: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Linked Lists, cont.

Page 27: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Linked Lists, cont.• Links, shown as arrows in the previous

diagram, are implemented as references and are instance variables of the node type.

• The reference marked head is a variable of the node type which provides access to the first node in the linked list, but is not itself one of the nodes.

• Each node is an object of a class that has (at least) two instance variables:– the data– the link.

Page 28: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Linked Lists, cont.

Page 29: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Detecting the Last Node

• There must be means for detecting the last node.

• A link instance variable with the value null indicates the last node.

• A reference to the linked list with the value null indicates an empty linked list.

• The value of the link instance variable is tested using ==.

Page 30: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

A Linked List of Strings

Page 31: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Moving Down a Linked List

Page 32: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Adding a Node at the Start

Page 33: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

A Linked List Demonstration

Page 34: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Advancing to the Next Node

Page 35: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Adding a Node

Page 36: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Deleting a Node

Page 37: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Privacy Leaks

• A method such as getLink in class ListNode returns an instance variable which is a reference to a node, potentially defeating the private restriction of the instance variable.

• This problem can be remedied by making class ListNode a private inner class of class StringLinkedList.

Page 38: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Inner Classes

• An inner class is a class defined within another class.

• An inner class provides a solution to the privacy leak problem.

Page 39: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Defining an Inner Class

public class OuterClass

{

OuterClass_Instance_Variables

OuterClass_Methods

private class InnerClass

{

InnerClass_Instance_Variables

InnerClass_Methods

}

}

Page 40: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Access to Members

• The inner and outer classes’ methods have access to each other’s methods and instance variables, even when they are declared private.

Page 41: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Node Inner Classes

• By making the node class an inner class, data structure classes become self-contained.

• Further, the accessor and mutator methods of the inner class can be eliminated since instance variables of an inner class are accessible directly.

Page 42: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Node Inner Classes, cont.

Page 43: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Node Inner Classes, cont.• class StringLinkedListSelfContained, cont.

Page 44: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Other Linked Data Structures

• Many colleges and universities offer a course devoted entirely to the study of data structures.

• Typically, a data structure can be implemented by “linking” its elements together.

• Example data structures include stacks, queues, deques, trees, binary trees, graphs, and directed graphs.– Refer java.util package

Page 45: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Introduction to Generics

• Java 5.0 allows definitions, called generics, that include parameters for types.

• Generics can be subtle and full of pitfalls.• We provide an introduction to generics.• Serious programming with generics is

presented in more advanced texts.

Page 46: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Generic Basics

• Classes and methods can have a type parameter.

• Any class type can be substituted for the type parameter, producing a specific class type or method.

Page 47: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

• class Sample<T>

Generic Basics, cont.

Page 48: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Generic Basics, cont.

• A class definition with a type parameter is stored in a file and compiled just like any other class.

• When used in code a class type must be specified so that it can be substituted for the type parameter.

Page 49: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Generic Basics, cont.

• exampleSample<String> 01 = new Sample<String>();

o1. setData(“Hello”);

Sample<Species> ow = new Sample<Species>();

Species x = new Species();

<code to set the data for object s>o2.setData(s);

Page 50: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Generic Basics, cont.

• You cannot substitute a primitive type for a type parameter.

• You must instead use a class type.

Page 51: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Programming Example: A Generic Linked List

Page 52: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Programming Example: A Generic Linked List, cont.

Page 53: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Programming Example: A Generic Linked List, cont.

Page 54: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Programming Example: A Generic Linked List, cont.

Page 55: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Generic Constructor

• The class name in a parameterized class definition has a type parameter attached.

• But, a generic constructor name has no type parameter and the type parameter is not used in the heading of the constructor definition.public LinkedList()

notpublic LinkedList<e>() // ILLEGAL

Page 56: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Limited Use of the Type Parameter

• Within the definition of a parameterized class definition, there are places where a type name is allowed, but a type parameter is not allowed.

• Type parameters cannot be used in simple expressions that use new to create a new object.

Page 57: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Limited Use of the Type Parameter

• examplesT object = new T(); // ILLEGAL

T[] a = new T[10]; // ILLEGAL

– In both cases, the first T is legal, but the second T is illegal.

Page 58: Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics

Summary

• You have become familiar with vectors.• You have learned about linked data structures

in Java.• You have learned how to manipulate linked

lists.• You have learned about generics (parameters

for types).