Section 4 (corrected)

advertisement
Section 4

Boxing classes

Array initialization

Lists: recursion and iteration
Boxing classes

Many library methods have Objects
as parameters, i.e.
add(Object o)

However, primitive types, like int,
double, float etc. are not objects, not
to mention Objects.

Solution: Boxing/wrapper classes
Note: Cornell’s boxing classes: http://www.pe.cornell.edu/physed/martial-f03.html
Wrapper classes

For each primitive type there is
a corresponding wrapper class:
Primitive type
Wrapper class
int
Integer
float
Float
char
Character
...
...
Wrapper classes

Usage:
int k = 23;
Integer i = new Integer(k);
k = i.intValue();
float f = 5.17;
Float g = new Float(f);
f = g.floatValue();
See Java documentation for all
methods!
Integer
int
int
Float
float
intValue
floatValue
float
Wrapper classes
Every Java class inherits from
Object class.
 We can therefore use Integer,
Char (and any other) object
whenever a method expects an
object.
 For example, we can call
add(new Integer(10));

Wrapper classes - usage

Java container classes store
Objects.

TreeSet s = new TreeSet;

s.add(new Integer(7));

We cannot store ints directly!
Array initialization

In Java (or C++), when an array of
objects using new, array elements are
not initialized automatically!
Integer []arr = new Integer[100];

arr contains 100 nulls!
for (int i = 0; i < 100; i++)
arr[i] = new Integer(i);

Now every element of an array is
initialized.
Array initialization

The same thing happens during
creation of multidimensional
arrays:
Object [][]arr = new Object[17][23];
for (int i = 0; i < 17; i++)
for (int j = 0; j < 23; j++)
arr[i][j] = new Integer(i*j);
Array initialization moral
ALWAYS
initialize array
elements.
Lists

Cells containg Objects. Each Cell
has a pointer to the next cell in the
list.
class ListCell
{
...
public ListCell getNext();
// returns the next element.
public void setNext(ListCell l);
// sets the pointer to point to l
public Object getDatum();
// returns the object stored in the cell
}
Lists: Iteration and
recursion
class Course
{
public Book textBook;
...
}
class Student
{
private ListCell courses;
public void readBook(Book b)
{...}
private void study() {???}
}
Lists

Suppose that student studies by
reading all textbooks for all
his/her courses.

How does a study method look
like?
Lists: Iteration

Iterative version:
public void study()
{
for (ListCell current = courses; current != null;
current = current.getNext())
readBook(((Course)current.getDatum()).textBook;
}

We simply go through a list doing
something to each element.
CS 211
current
CS 212
CS 611
CS 711
null
Lists: Iteration

Iterative version:
public void study()
{
for (ListCell current = courses; current != null;
current = current.getNext())
readBook(((Course)current.getDatum()).textBook;
}

We simply go through a list doing
something to each element.
CS 211
CS 212
current
CS 611
CS 711
null
Lists: Iteration

Iterative version:
public void study()
{
for (ListCell current = courses; current != null;
current = current.getNext())
readBook(((Course)current.getDatum()).textBook;
}

We simply go through a list doing
something to each element.
CS 211
CS 212
CS 611
current
CS 711
null
Lists: Iteration

Iterative version:
public void study()
{
for (ListCell current = courses; current != null;
current = current.getNext())
readBook(((Course)current.getDatum()).textBook;
}

We simply go through a list doing
something to each element.
CS 211
CS 212
CS 611
CS 711
current
null
Lists: Iteration

Iterative version:
public void study()
{
for (ListCell current = courses; current != null;
current = current.getNext())
readBook(((Course)current.getDatum()).textBook;
}

We simply go through a list doing
something to each element.
CS 211
CS 212
CS 611
CS 711
null
current
Lists: recursion
private void study1(ListCell c)
{
if (c == null)
return;
readBook(((Course)c.getDatum()).textBook;
study1(c.next());
}
CS 211
CS 212
CS 611
CS 711
null
Lists: recursion
private void study1(ListCell c)
{
if (c == null)
return;
readBook(((Course)c.getDatum()).textBook;
study1(c.next());
}
CS 212
CS 611
CS 711
null
Lists: recursion
private void study1(ListCell c)
{
if (c == null)
return;
readBook(((Course)c.getDatum()).textBook;
study1(c.next());
}
CS 611
CS 711
null
Lists: recursion
private void study1(ListCell c)
{
if (c == null)
return;
readBook(((Course)c.getDatum()).textBook;
study1(c.next());
}
CS 711
null
Lists: recursion
private void study1(ListCell c)
{
if (c == null)
return;
readBook(((Course)c.getDatum()).textBook;
study1(c.next());
}
null
Lists: recursion
private void study1(ListCell c)
{
if (c == null)
return;
readBook(((Course)c.getDatum()).textBook;
study1(c.next());
}

Note that if we wanted to print
out the name of each course the
code would look almost exactly
the same, but we need to write it
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
occurence of the Object o from
the List l and return the
modified list:
ListCell delete(ListCell l, Object
o)
{???}

Lists: Iteration

Deleting element: iterative
version.

Intuition: We look for o in the
list in a loop, once we found it
we update the list and return.
Lists: Iteration
ListCell delete(ListCell l, Object o)
{
ListCell current = l, previous = null;
while (current != null)
{
if (current.getDatum().equals(o)) // found the object
{
if (previous == null) return l.getNext() ;
// it was the first one
else
{ previous.setNext(current.getNext()); return l; }
}
else
previous = current;
current = current.getNext();
}
return l;
}

Difficult to understand!
Lists: Recursion

Deleting element: recursive
way:
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:
ListCell delete(ListCell l, Object o)
{
if (l == null) return l;
if (l.getDatum().equals(o)) return l.getNext();
l.setNext(delete(l.getNext(), o);
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
better when you use recursion.
Download