Iterators Flashcards

1
Q

What is an Iterator?

A

Iterators are (usually) a special class/object that allows the user to easily traverse a data structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What kind of methods does an Iterator have?

A

Iterators usually have methods to get the current item, go to the next item, or go to the previous item.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is an Iterator used?

A

The data structure itself implements the iterator (usually as an inner class or a private class).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What needs to be done to have an Iterator in your class?

A
The class itself would implement the
Iterable interface (in the java.lang package), which has
just one method: iterator(). This method returns an
instance of the iterator for that data structure.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What methods does the Iterator interface have?

A

The Iterator interface in Java has three methods:

hasNext(), next(), and remove().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the hasNext() method do?

A

hasNext() tells you whether calling next() will return the
next item in the data structure (i.e. if there is at least one
more item).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the next() method do?

A

next() returns the next item in the data structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the remove() method do?

A

remove() removes the most-recently returned item.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly