¡GoConqr estrena diseño! Cuéntanos qué te parece.

Apuntes

Slide Notes Chapter 15 Iterators CSIS B

Study Hard Computer Science Inglés 11 páginas

Contenido del apunte (11)

Página 1

What is an iterator?

It is something that allows you to step through and traverse a collection of data.

It is like a pointer or a cursor, it goes through looking at each thing on the list.

There are different types of Iterators who have their own classes.

Separate class iterator

Inner class iterator (array-based implementation, linked implementation)

Implementing this interface allows an object to be the target of the "for-each loop" statement

What is a for-loop?

ex:

for (Suit suit : suits)

{

for(Rank rank : ranks)

{

sortedDeck.add(new Card(suit, rank));

}

}

The for-each construct is tailor-made for nested iteration! aka iterator classes

Página 2

We use iterators to traverse and manipulate a list

How do we use iterator methods in our classes?

Example: Say we had:

ListInterface nameList = new LList();

we add names to the list: nameList.add("Jamie");

nameList.add("Joe");

nameList.add("Rachel");

We use an instance of separateIterator

Iterator nameIterator = new SeparateIterator (nameList);

What just happen was that nameIterator got connected to the list nameList

Página 3

Iterators have a next method and a hasNext method

hasNext() is a boolean it returns true or false

next() returns the next data and advances the iterator (goes to the next thing)

Explain what the following do if we have Jamie, Joey and Rachel on the list

Iterator nameIterator = new SeparateIterator(nameList);

nameIterator.next(); ---------------------> looks at Jamie

nameIterator.next() -----------------------> looks at Joey

nameIterator.remove() -------------------> removes Joey

System.out.println(nameIterator.hasNext()); ---------> Checks to see if there is something after Rachel, yes returns true

System.out.println(nameIterator.next()); looks at Rachel, prints Rachel

Página 4

If a String had three entries in it and you wanted to use the iterator to print the third String, how would you print it?

nameIterator.next();

nameIterator.next();

System.out.println(nameIterator.next());

Página 5

Write statements that display the even-numbered entries in the list. Displaying the second entry, fourth entry and so on.

nameIterator.next(); -----------------------------------------> we do this to skip the first entry

while (nameIterator.hasNext()) ---------------------------> while the next one has a next one

{

System.out.println(nameIterator.next()); -----------> prints the one after nameIterator.hasNext()

if (nameIterator.hasNext()) -----------------------------> if there is a next one

{

nameIterator.next(); -----------------------------------> that next one becomes nameIterator.next(), advancing the iterator while skipping the odds.

}

}

Página 6

Using an iterator, how would you remove all the entries from the list?

while(nameIterator.hasNext())

{

nameIterator.next();

nameIterator.remove();

}

Página 7

We can do multiple iterators and have them going through the same data at the same time.

We can do this however we want. Some people have an iterator go in through the front while having a different iterator go in through the back, in search of specific data.

How do we implement: SeparateIterator

We implement the interface : jave.util. Iterator

If a list is empty, what does the method hasNext return?

It returns false

This is because both nextPosition and list.getLength() are zero

Página 8

What would take longer to do an iteration through: Linked list or Array?

The linked list will take longer. The particular implementation of the list affects the amount of work that the method getEntry must perform. For an array-based implementation, getEntry accesses the required entry directly and immediately. For linked implementation, getEntry must traverse a chain of nodes to find the desired entry. This takes more time to accomplish than accessing an array entry.

Given the class LinkedListWithIterator, what java statements create the iterators nae Iterator and countingterator mentioned?

Iterator<String> nameIterator = nameList.getIterator();

Iterator<String> countingIterator = nameList.getiterator():

Página 9

What changes would be necessary to the methods in the inner class IteratorForArrayList if its constructor set nextIndex to: -1 instead of 0?

Originally, nextIndex is the index of the next entry that next will return. The change makes nextIndex the index of the last entry that next returned. Thus, the following changes are needed:

hasNext should compare nextIndex to numberOfEntries -1. Instead of numberOfEntries.

next should increment nextIndex before accessing list [nextInex]

remove should remove the entry at nextIndex + 1

Página 10

Inner class iterators have direct access to structure containing ADT's data

Suppose that you want to omit the method resetTraversal. Could the dafault constructor initialize nextNode to firstNode?

No. the default constructor creates an empty list. If it set nextNode to firstNode, nextNode would be set to null.

Could the add methods initialize nextNode to firstNode?

Yes, but with a disadvantage. Each addition to the list would set nextNode to firstNode. After creating a list, you could traverse it. However, the only way you could reset the traversal to the list's beginning would be to add another entry to the list.

Página 11

ListIterators have:

remove

add

set

next

nextIndex()

hasNext()

previousIndex()

hasPrevious()

REMEMBER: WE CANNOT CALL .remove() TWICE IN A ROW OR IF IT HAS NOTHING BEFORE IT. ALSO, WE CANNOT USE .add() BEFORE, THEN CALLING .remove() IMMEDIATELY AFTER

We have to have .next() or .previousIndex() before the .remove().

Información

Descripción
Computer Science Apunte sobre Slide Notes Chapter 15 Iterators CSIS B, creado por Study Hard el 16/04/2018.
Creado por
Study Hard
Visitas
1
Etiquetas
java b
Historial
  • Creado por Study Hard · 15 abr 2018
  • Copiado por Study Hard · 16 abr 2018
Valoración

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?