Array Iterators

21
Array Iterators arrays in Java do not implement the Iterable interface; hence, arrays do not supply iterators if you implement a class that uses an array to hold a collection of objects and you want to provide an iterator for the collection, you need to implement your own iterator class suppose we have a class X that has an array of Y objects 1

description

Array Iterators. arrays in Java do not implement the Iterable interface; hence, arrays do not supply iterators if you implement a class that uses an array to hold a collection of objects and you want to provide an iterator for the collection, you need to implement your own iterator class - PowerPoint PPT Presentation

Transcript of Array Iterators

Page 1: Array Iterators

Array Iterators arrays in Java do not implement the Iterable

interface; hence, arrays do not supply iterators

if you implement a class that uses an array to hold a collection of objects and you want to provide an iterator for the collection, you need to implement your own iterator class

suppose we have a class X that has an array of Y objects

1

Page 2: Array Iterators

public class X implements Iterable<Y>

{

private Y[] yArray;

// default constructor; creates yArray with

// length 0

public X()

{

this(0);

}

2

Page 3: Array Iterators

// constructor; creates yArray with

// length numberOfY

public X(int numberOfY)

{

if(numberOfY < 0)

throw IllegalArgumentException("negative

numberOfY");

this.yArray = new Y[numberOfY];

}

// …

3

Page 4: Array Iterators

YIterator because X implements Iterable, we must

supply a method named iterator that supplies an iterator to a Y object

we will call the class that defines the iterator to a Y object YIterator

4

Page 5: Array Iterators

import java.util.Iterator;

import java.util.NoSuchElementException;

class YIterator implements Iterator<Y>

{

// attributes and constructors coming up...

// Returns true if the iteration has more

// elements.

public boolean hasNext()

{ // ... }

5

Page 6: Array Iterators

// Returns the next element in the iteration.

public Y next()

{ // ... }

// Removes from the underlying collection the last

// element returned by the iterator (optional

// operation).

public void remove()

{ // ... }

}

6

Page 7: Array Iterators

Attributes how does a YIterator

know if there are more elements? return an element?

YIterator needs an attribute that holds the reference or a copy of

the array it is iterating over an attribute that allows the iterator to return the

next element recall that array elements are indexed using an integer

between 0 and (array length – 1)

7

Page 8: Array Iterators

Version 1: Reference to array class YIterator implements Iterator<Y>

{

private Y[] yArray;

private int nextIndex;

// precondition: arrayOfY not null

public YIterator(Y[] arrayOfY)

{

this.yArray = arrayOfY;

this.nextIndex = 0;

}

8

Page 9: Array Iterators

Version 2: Copy of array class YIterator implements Iterator<Y>

{

private Y[] yArray;

private int nextIndex;

// precondition: arrayOfY not null

public Yiterator(Y[] arrayOfY)

{

this.yArray = Arrays.copyOf(arrayOfY,

arrayOfY.length);

this.nextIndex = 0;

}

9

Page 10: Array Iterators

hasNext hasNext returns true if

the array yArray has an element at index nextIndex and

the element at index nextIndex is not null recall that every array has a public attribute

called length that gives the number of elements in the array the array yArray has an element at index nextIndex only if nextIndex < yArray.length

10

0 1 2 3 4

yArray yArray.length == 5

if nextIndex > 4 yArray[nextIndex] throwsArrayIndexOurOfBoundsException

Page 11: Array Iterators

Array Memory Diagram for Objects

11

500 Y[] object

length 5

yArray[0]

yArray[1]

yArray[2]

yArray[3]

yArrat[4]

references to objects of type Y

Y[] yArray = new Y[5];for(int i = 0; i < yArray.length; ++i) yArray[i] = new Y();

null

null

null

null

null

1600

1500

1400

1300

1200

Page 12: Array Iterators

Array Memory Diagram for Objects

12

500 fArray object

length 5

fArray[0]

fArray[1]

fArray[2]

fArray[3]

fArrat[4]

instances of type int

float[] fArray = new float[5];for(int i = 0; i < yArray.length; ++i) fArray[i] = (float)i;

0

0

0

0

0

4f

3f

2f

1f

0f

Page 13: Array Iterators

// verbose version

public boolean hasNext()

{

boolean result = false;

if(this.nextIndex < this.yArray.length &&

this.yArray[nextIndex] != null)

{

result = true;

}

return result;

}

13

Page 14: Array Iterators

// compact version

public boolean hasNext()

{

return this.nextIndex < this.yArray.length &&

this.yArray[nextIndex] != null;

}

14

Page 15: Array Iterators

if hasNext returns true then the client can call next to get the next element in the iteration if hasNext returns false and the client calls next a NoSuchElementException is thrown

the next method needs to1. check that hasNext is true; if not an exception is

thrown2. get the element at index nextIndex from the array

yArray3. move nextIndex to the next element in the array

(increment)

15

Page 16: Array Iterators

// verbose version public Y next() { if(this.hasNext()) { Y nextY = this.yArray[this.nextIndex]; this.nextIndex++; return nextY; } else { throw new NoSuchElementException("no more elements"); } }

16

Page 17: Array Iterators

// compact version public Y next() { if(this.hasNext()) { return this.yArray[this.nextIndex++]; } else { throw new NoSuchElementException("no more elements"); } }

17

Page 18: Array Iterators

remove remove is an optional operation

removes from the underlying collection the last element returned by the iterator the client must first call the next method (to return an

element) and then call remove not as easy as removing the element indexed by nextIndex

if the iterator does not support remove it must throw an UnsupportedOperationException

18

Page 19: Array Iterators

public void remove()

{

throw new

UnsupportedOperationException("Iterator does

not support remove");

}

19

Page 20: Array Iterators

Removing an Element from an Array removing an element from an array should

reduce the length of the array by 1 Java does not support resizable arrays

need to create a new array that is missing the removed element

20

500 Y[] object

length 5

1200

1300

remove 1400

1500

1600

2000 Y[] object

length 4

1200

1300

1500

1600

Page 21: Array Iterators

A Question suppose your iterator class supports remove

and copies the array it is iterating over (slide 9) what problem do you need to address when

implementing remove?

21