CS61B Lecture #3: Containers in Java, Slides of Data Structures and Algorithms

A part of the cs61b lecture notes on containers in java. It covers the basics of values and containers, pointers, and the difference between destructive and non-destructive operations. It also explains how to define new types of objects using class declarations and provides examples of primitive operations and destructive vs. Non-destructive methods.

Typology: Slides

2012/2013

Uploaded on 04/27/2013

netii
netii 🇮🇳

4.4

(7)

91 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS61B Lecture #3: Containers
Readers Available: from
Vick Copy
(corner of Euclid and Hearst)
not
Copy Central! Also online.
Please read Chapter 2 of
Assorted Materials on Java
from the
reader.
Room change: Discussion 114 (3-4 Thurs.) is now in 289 Cory (used
to be 3111 Etch.)
Midterm is tentatively scheduled for the evening of 9 March (Thurs-
day).
Project 1 will be due the preceding week (1 March).
Today. Simple classes. Scheme-like lists. Destructive vs. non-
destructive operations. Models of memory.
Last modified: Mon Jan 23 14:30:41 2006 CS61B: Lecture #3 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS61B Lecture #3: Containers in Java and more Slides Data Structures and Algorithms in PDF only on Docsity!

CS61B Lecture #3: Containers

  • Readers Available: from Vick Copy (corner of Euclid and Hearst) not Copy Central! Also online.
  • Please read Chapter 2 of Assorted Materials on Java from the reader.
  • Room change: Discussion 114 (3-4 Thurs.) is now in 289 Cory (used to be 3111 Etch.)
  • Midterm is tentatively scheduled for the evening of 9 March (Thurs- day).
  • Project 1 will be due the preceding week (1 March).
  • Today. Simple classes. Scheme-like lists. Destructive vs. non- destructive operations. Models of memory.

Values and Containers

  • Values are numbers, booleans, and pointers. Values never change.

3 ’a’ true

  • Simple containers contain values:

x: 3 L: p:

Examples: variables, fields, individual array elements, parameters.

  • Structured containers contain (0 or more) other containers:

h t

h: 3 t:

0 17

1 9

2

Class Object Array Object Empty Object

Alternative Notation

Containers in Java

  • Containers may be named or anonymous.
  • In Java, all simple containers are named, all structured contain- ers are anonymous, and pointers point only to structured containers. (Therefore, structured containers contain only simple containers).

p: 3

h t 7

h t

simple container (local variable)

structured containers (anonymous)

named simple containers (fields) within structured containers

  • In Java, assignment copies values into simple containers.
  • Exactly like Scheme!

Defining New Types of Object

  • Class declarations introduce new types of objects.
  • Example: list of integers:

public class IntList {

// Constructor function

// (used to initialize new object)

/** List cell containing (HEAD, TAIL). */

public IntList (int head, IntList tail) {

this.head = head; this.tail = tail;

// Names of simple containers ( fields)

public int head;

public IntList tail;

Destructive vs. Non-destructive

Problem: Given a (pointer to a) list of integers, L, and an integer in-

crement n, return a list created by incrementing all elements of the list

by n.

/** List of all items in P incremented by n. */

static IntList incrList (IntList P, int n) {

if (P == null)

return null;

else return new IntList (P.head+n, incrList(P.tail, n));

We say incrList is non-destructive, because it leaves the input objects

unchanged, as shown on the left. A destructive method may modify the input objects, so that the original data is no longer available, as shown on the right:

L:

Q:

After Q = incrList(L, 2):

L:

Q:

After Q = dincrList(L, 2) (destructive):

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.

Easier to build things first-to-last, unlike recursive version:

static IntList incrList (IntList P, int n) { if (P == null) return null; IntList result, last; result = last = new IntList (P.head+n, null); while (P.tail != null) { P = P.tail; last.tail = new IntList (P.head+n, null); last = last.tail; } return result; }

P: 3 43 56

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.

Easier to build things first-to-last, unlike recursive version:

static IntList incrList (IntList P, int n) { if (P == null) return null; IntList result, last; result = last = new IntList (P.head+n, null); while (P.tail != null) { P = P.tail; last.tail = new IntList (P.head+n, null); last = last.tail; } return result; }

P: 3 43 56

last:

result: 5

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.

Easier to build things first-to-last, unlike recursive version:

static IntList incrList (IntList P, int n) { if (P == null) return null; IntList result, last; result = last = new IntList (P.head+n, null); while (P.tail != null) { P = P.tail; last.tail = new IntList (P.head+n, null); last = last.tail; } return result; }

P: 3 43 56

last:

result: 5 45

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.

Easier to build things first-to-last, unlike recursive version:

static IntList incrList (IntList P, int n) { if (P == null) return null; IntList result, last; result = last = new IntList (P.head+n, null); while (P.tail != null) { P = P.tail; last.tail = new IntList (P.head+n, null); last = last.tail; } return result; }

P: 3 43 56

last:

result: 5 45

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.

Easier to build things first-to-last, unlike recursive version:

static IntList incrList (IntList P, int n) { if (P == null) return null; IntList result, last; result = last = new IntList (P.head+n, null); while (P.tail != null) { P = P.tail; last.tail = new IntList (P.head+n, null); last = last.tail; } return result; }

P: 3 43 56

last:

result: 5 45 58