Section 7 topics - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Section 7 topics

Description:

We can return an instance of inner class to the outside world. ... public void seta(int q) { a = q; } public class B. public int geta() { return a; ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 26
Provided by: isci
Category:
Tags: class | section | topics

less

Transcript and Presenter's Notes

Title: Section 7 topics


1
Section 7 topics
  • Prelim review.
  • Inner classes.
  • Iterators.

2
Prelim review, problem 1
C1
I1
C2
I2
C3
3
Prelim review, problem 1
C1 obj1
obj3 obj2
obj2 obj1
I1 i1
C2 obj2
i1 obj3
obj3 obj1
I2 i2
C3 obj3
i2 obj1
4
Prelim review, problem 4
  • Reversing a list without allocating new objects.
  • Use iteration!
  • Keep track of the current object and the previous
    object.

5
Prelim review, problem 4
  • static public ListCell reverse(ListCell f)
  • ListCell current, previous
  • // check two base cases
  • if (f null) return null
  • if (f.getNext() null) return f
  • // set the initial values of the
  • // variables
  • current f.getNext()
  • previous f
  • previous.setNext(null)
  • while (current ! null)
  • ListCell temp current.getNext()
  • current.setNext(previous)
  • previous current
  • current temp
  • return previous

6
Prelim review,problem 6
  • Interesting (?) question find a general
    algorithm which given two lists decides whether
    there is a tree, which postorder traversal
    results in the first list, and preorder in the
    second one.
  • Even more interesting (??) question Find one
    which is efficient.

7
Iterators
  • Abstract concept Containers.
  • Instances Arrays, trees, sets, lists, datasets
    itp.
  • Associated concept Iterators.
  • Used to iterate over a container.
  • I.e. suppose we want to find out the number of
    the elements in any container.

8
Iterators
  • We would like to have a way to write even more
    generic functions.
  • I.e. suppose we want to find out the number of
    the elements in any container.
  • Or given a container, create a list of its
    elements.

9
Iterators
  • With a list
  • int elems(ListCell l)
  • int i 0
  • for (ListCell q l q ! null q q.getNext())
  • i
  • return i

10
Iterators
  • With a tree
  • int elems(TreeCell t)
  • if (t null) return 0
  • return elems(t.getLeft()) elems(t.getRight())
    1

11
Iterators
  • More general solution - define an interface of
    Iterator
  • interface Iterator
  • boolean hasNext()
  • Object next()
  • void remove()

12
Iterators
  • Now we can write (note the usage, which is
    typical)
  • int elems(Iterator i)
  • Object o
  • int res 0
  • while (i.hasNext())
  • o i.next()
  • // here we could do something with o
  • res
  • return res

13
Iterators
  • How to implement them?
  • Consider the class
  • class ArrayIntContainer
  • public int a
  • ...

14
Iterators
  • First idea Make the container class implement
    the iterator.
  • class ArrayIntContainer implements Iterator
  • public int a
  • private int counter
  • public boolean hasNext()
  • return counter lt a.length
  • public Object next()
  • return new Integer(acounter)
  • ArrayIntContainter() counter 0
  • Disadvantages?

15
Iterators
  • First idea Make the container class implement
    the iterator.
  • class ArrayIntContainer implements Iterator
  • public int a
  • private int counter
  • public boolean hasNext()
  • return counter lt a.length
  • public Object next()
  • return new Integer(acounter)
  • ArrayIntContainter() counter 0
  • Disadvantages No way to reset the counter. Only
    one iterator per object.

16
Iterators
  • Second idea Create the adapter class, used only
    for iteration, which keeps the reference of the
    container object.
  • class AIterator implements Iterator
  • private ArrayIntContainer aic
  • int counter
  • AIterator(ArrayIntContainer c)
  • aic c counter 0
  • public boolean hasNext()
  • return counter lt aic.a.length
  • ...
  • Disadvantages?

17
Iterators
  • Second idea Create the adapter class, used only
    for iteration, which keeps the reference of the
    container object.
  • class AIterator implements Iterator
  • private ArrayIntContainer aic
  • int counter
  • AIterator(ArrayIntContainer c)
  • aic c counter 0
  • public boolean hasNext()
  • return counter lt aic.a.length
  • ...
  • Disadvantages Doesnt work if a is private. Too
    large separation can result in incosistent code.

18
A solution!
  • A good solution is to use inner classes.

19
Inner classes
  • Important features
  • We can declare a class inside another class.
  • We can return an instance of inner class to the
    outside world.
  • The instance of the inner class keeps track of
    the outer class object.

20
Inner classes
  • class A
  • private int a
  • A() a 0
  • public void seta(int q) a q
  • public class B
  • public int geta() return a
  • public B getB() return new B()
  • public void method()
  • A a new A()
  • A.B b a.getB()
  • a.seta(20)
  • System.out.println(b.geta())

21
Back to the futureO Iterators
  • Inner classes allow us to implement iterators in
    a nice way.
  • class ArrayIntContainer
  • private int a
  • private class AICIterator implements Iterator
  • private int counter
  • public boolean hasNext()
  • return counter lt a.length
  • public Object next()
  • return new Integer(acounter)
  • AICIterator() counter 0
  • public Iterator getIterator()
  • return new AICIterator()

22
Generic functions
  • interface HasIterator
  • Iterator getIterator()
  • int elems(HasIterator i)
  • ...

23
Generic functions
  • interface HasIterator
  • Iterator getIterator()
  • int elems(HasIterator c)
  • Iterator i c.getIterator()
  • int res 0
  • while (i.hasNext())
  • res
  • i.next()
  • return res

24
Fancy stuff
  • Anonymous inner classes
  • public Iterator getIterator
  • return new Iterator
  • public boolean hasNext()
  • ...
  • ...
  • Saves the namespace.

25
Things to remember
  • General pattern of implementing adaptors - things
    which use class data, but are somehow separate
    from the class.
Write a Comment
User Comments (0)
About PowerShow.com