section 4 boxing classes boxing classes array initialization array initialization lists: recursion...

30
Section 4 Boxing classes Boxing classes Array initialization Array initialization Lists: recursion and Lists: recursion and iteration iteration

Upload: carmel-barrett

Post on 13-Dec-2015

251 views

Category:

Documents


0 download

TRANSCRIPT

Section 4

Boxing classesBoxing classes

Array initializationArray initialization

Lists: recursion and iterationLists: recursion and iteration

Boxing classes

Many library methods have Objects as Many library methods have Objects as parameters, i.e.parameters, i.e.

add(Object o)add(Object o)

However, primitive types, like int, However, primitive types, like int, double, float etc. are not objects, not double, float etc. are not objects, not to mention Objects.to mention Objects.

Solution: Boxing/wrapper classesSolution: Boxing/wrapper classes

Note: Cornell’s boxing classes: Note: Cornell’s boxing classes: http://www.pe.cornell.edu/physed/martial-http://www.pe.cornell.edu/physed/martial-ff03.html 03.html

Wrapper classes

For each primitive type there is For each primitive type there is a corresponding wrapper class:a corresponding wrapper class:

Primitive typePrimitive type Wrapper classWrapper class

intint IntegerInteger

floatfloat FloatFloat

charchar CharacterCharacter

...... ......

Wrapper classes

Usage:Usage:

int k = 23;int k = 23;Integer i = new Integer(k);Integer i = new Integer(k);k = i.intValue();k = i.intValue();

float f = 5.17;float f = 5.17;Float g = new Float(f);Float g = new Float(f);f = g.floatValue();f = g.floatValue();

See Java documentation for all See Java documentation for all methods!methods!

Integer

int int

intValue

Float

float float

floatValue

Wrapper classes

Every Java class inherits from Every Java class inherits from Object class.Object class.

We can therefore use Integer, We can therefore use Integer, Char (and any other) object Char (and any other) object whenever a method expects an whenever a method expects an object.object.

For example, we can call For example, we can call add(new Integer(10));add(new Integer(10));

Wrapper classes - usage

Java container classes store Java container classes store Objects.Objects.

TreeSet s = new TreeSet;TreeSet s = new TreeSet;

s.add(new Integer(7));s.add(new Integer(7));

We cannot store ints directly!We cannot store ints directly!

Array initialization

In Java (or C++), when an array of In Java (or C++), when an array of objects using new, array elements are objects using new, array elements are not initialized automatically!not initialized automatically!

Integer []arr = new Integer[100];Integer []arr = new Integer[100];

arr contains 100 nulls! arr contains 100 nulls!

for (int i = 0; i < 100; i++)for (int i = 0; i < 100; i++) arr[i] = new Integer(i);arr[i] = new Integer(i);

Now every element of an array is Now every element of an array is initialized.initialized.

Array initialization

The same thing happens during The same thing happens during creation of multidimensional creation of multidimensional arrays:arrays:

Object [][]arr = new Object[17][23];Object [][]arr = new Object[17][23];

for (int i = 0; i < 17; i++)for (int i = 0; i < 17; i++)

for (int j = 0; j < 23; j++)for (int j = 0; j < 23; j++)

arr[i][j] = new Integer(i*j);arr[i][j] = new Integer(i*j);

Array initialization - moral

ALWAYS ALWAYS initialize array initialize array

elements. elements.

Lists

Cells containg Objects. Each Cell has a Cells containg Objects. Each Cell has a pointer to the next cell in the list.pointer to the next cell in the list.

class ListCellclass ListCell{{ ...... public ListCell getNext(); public ListCell getNext(); // returns the next element. // returns the next element. public void setNext(ListCell l);public void setNext(ListCell l); // sets the pointer to point to l// sets the pointer to point to l public Object getDatum();public Object getDatum(); // returns the object stored in the cell// returns the object stored in the cell}}

Lists: Iteration and recursion

class Courseclass Course{{ public Book textBook;public Book textBook; ......}}

class Studentclass Student{ { private ListCell courses;private ListCell courses;

public void readBook(Book b)public void readBook(Book b) {...}{...}

private void study() {???}private void study() {???} }}

Lists

Suppose that student studies by Suppose that student studies by reading all textbooks for all reading all textbooks for all his/her courses.his/her courses.

How does a study method look How does a study method look like?like?

Lists: Iteration

Iterative version:Iterative version:public void study()public void study()

{{

for (ListCell current = courses; current != null; for (ListCell current = courses; current != null;

current = current.getNext())current = current.getNext())

readBook(((Course)current.getDatum()).textBook;readBook(((Course)current.getDatum()).textBook;

}}

We simply go through a list doing We simply go through a list doing something to each element.something to each element.

current

CS 211 CS 212 CS 611 CS 711 null

Lists: Iteration

Iterative version:Iterative version:public void study()public void study()

{{

for (ListCell current = courses; current != null; for (ListCell current = courses; current != null;

current = current.getNext())current = current.getNext())

readBook(((Course)current.getDatum()).textBook;readBook(((Course)current.getDatum()).textBook;

}}

We simply go through a list doing We simply go through a list doing something to each element.something to each element.

current

CS 211 CS 212 CS 611 CS 711 null

Lists: Iteration

Iterative version:Iterative version:public void study()public void study()

{{

for (ListCell current = courses; current != null; for (ListCell current = courses; current != null;

current = current.getNext())current = current.getNext())

readBook(((Course)current.getDatum()).textBook;readBook(((Course)current.getDatum()).textBook;

}}

We simply go through a list doing We simply go through a list doing something to each element.something to each element.

current

CS 211 CS 212 CS 611 CS 711 null

Lists: Iteration

Iterative version:Iterative version:public void study()public void study()

{{

for (ListCell current = courses; current != null; for (ListCell current = courses; current != null;

current = current.getNext())current = current.getNext())

readBook(((Course)current.getDatum()).textBook;readBook(((Course)current.getDatum()).textBook;

}}

We simply go through a list doing We simply go through a list doing something to each element.something to each element.

CS 211 CS 212 CS 611 CS 711

current

null

Lists: Iteration

Iterative version:Iterative version:public void study()public void study()

{{

for (ListCell current = courses; current != null; for (ListCell current = courses; current != null;

current = current.getNext())current = current.getNext())

readBook(((Course)current.getDatum()).textBook;readBook(((Course)current.getDatum()).textBook;

}}

We simply go through a list doing We simply go through a list doing something to each element.something to each element.

current

CS 211 CS 212 CS 611 CS 711 null

Lists: recursion

private void study1(ListCell c)private void study1(ListCell c)

{{

if (c == null)if (c == null)

return;return;

readBook(((Course)c.getDatum()).textBook;readBook(((Course)c.getDatum()).textBook;

study1(c.next());study1(c.next());

}}

CS 211 CS 212 CS 611 CS 711 null

Lists: recursion

private void study1(ListCell c)private void study1(ListCell c)

{{

if (c == null)if (c == null)

return;return;

readBook(((Course)c.getDatum()).textBook;readBook(((Course)c.getDatum()).textBook;

study1(c.next());study1(c.next());

}}

CS 212 CS 611 CS 711 null

Lists: recursion

private void study1(ListCell c)private void study1(ListCell c)

{{

if (c == null)if (c == null)

return;return;

readBook(((Course)c.getDatum()).textBook;readBook(((Course)c.getDatum()).textBook;

study1(c.next());study1(c.next());

}}

CS 611 CS 711 null

Lists: recursion

private void study1(ListCell c)private void study1(ListCell c)

{{

if (c == null)if (c == null)

return;return;

readBook(((Course)c.getDatum()).textBook;readBook(((Course)c.getDatum()).textBook;

study1(c.next());study1(c.next());

}}

CS 711 null

Lists: recursion

private void study1(ListCell c)private void study1(ListCell c)

{{

if (c == null)if (c == null)

return;return;

readBook(((Course)c.getDatum()).textBook;readBook(((Course)c.getDatum()).textBook;

study1(c.next());study1(c.next());

}}

null

Lists: recursion

private void study1(ListCell c)private void study1(ListCell c)

{{

if (c == null)if (c == null)

return;return;

readBook(((Course)c.getDatum()).textBook;readBook(((Course)c.getDatum()).textBook;

study1(c.next());study1(c.next());

}}

Note that if we wanted to print Note that if we wanted to print out the name of each course the out the name of each course the code would look almost exactly code would look almost exactly the same, but we need to write it the same, but we need to write it anyway. anyway.

Functional programming ad:

study = mapM_ readBook courses

printCourses = mapM_ print courses

We can abstract the idea of iteration and actually write a function mapM_!

Deleting from lists

We want delete the first We want delete the first occurence of the Object o from occurence of the Object o from the List l and return the the List l and return the modified list:modified list:

ListCell delete(ListCell l, Object ListCell delete(ListCell l, Object o)o)

{ ? ? ? }{ ? ? ? }

Lists: Iteration

Deleting element: iterative Deleting element: iterative version.version.

Intuition: We look for o in the Intuition: We look for o in the list in a loop, once we found it list in a loop, once we found it we update the list and return.we update the list and return.

Lists: Iteration

ListCell delete(ListCell l, Object o)ListCell delete(ListCell l, Object o){{ ListCell current = l, previous = null;ListCell current = l, previous = null; while (current != null)while (current != null) { { if (current.getDatum().equals(o)) // found the objectif (current.getDatum().equals(o)) // found the object { { if (previous == null) return l.getNext() ; if (previous == null) return l.getNext() ; // it was the first one// it was the first one elseelse { previous.setNext(current.getNext()); return l; }{ previous.setNext(current.getNext()); return l; } } } elseelse previous = current;previous = current; current = current.getNext();current = current.getNext(); }} return l;return l;}}

Difficult to understand!Difficult to understand!

Lists: Recursion

Deleting element: recursive Deleting element: recursive way:way:

Intuition:Intuition: – If list l is empty, return null. – If first element of l is o, return

rest of list l. – Otherwise, return list

consisting of first element of l, and the list that results from deleting o from the rest of list l.

Lists: Recursion

Deleting an element from list:Deleting an element from list:ListCell delete(ListCell l, Object o)ListCell delete(ListCell l, Object o)

{{

if (l == null) return l;if (l == null) return l;

if (l.getDatum().equals(o)) return l.getNext();if (l.getDatum().equals(o)) return l.getNext();

l.setNext(delete(l.getNext(), o);l.setNext(delete(l.getNext(), o);

return l;return l;

}}

Functional programming ad:

delete [] o = []

delete (x:xs) o = if x == o then xs else (x: (delete xs o ))

Lists: Moral

Many functions on lists look Many functions on lists look better when you use recursion.better when you use recursion.