L6_Iterator

L6_Iterator - Iterators In this lecture we shall take a...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Iterators In this lecture we shall take a detour from data structures, and introduce an advanced feature in Java: the iterators. Page 1 of 18 Iterators Consider a symbol table that is implemented using hash table with separate chaining. Task To print out all keys in this table. "Miss Chan" … … "Duckling" "Goosy" "Principal" "Waiter" "Mrs Mak" … Page 2 of 18 A Symbol Table Key Iterator is an object returned by a Symbol Table operation. public interface SymbolTable { public void enter(String key, Object value); public Object lookup(String key); public SymTabKeyIterator keyIterator(); } Page 3 of 18 The implementation of the new operation is simple … public class SymbolTable_v1 implements SymbolTable { … public void enter(String key, Object value) { … } public Object lookup(String key) { … } public SymTabKeyIterator keyIterator() { return new SymTabKeyIterator(this); } } Page 4 of 18 Note z public SymTabKeyIterator keyIterator() { return new SymTabKeyIterator(this); } "Miss Chan" "Duc ng" "Goosy" "Princi al" "Waiter" "Mrs Mak" table1 SymbolTable_v1 it1 SymTabKeyIterator it1; SymbolTable table1; … … table1 = new SymbolTable_v1(); table1.enter(…); … … it1 = table1.keyIterator(); Page 5 of 18 The SymTabKeyIterator Class An iterator should have two methods: SymbolTable_v1 table1 "Miss Chan" "Duc ng" "Goosy" "Princi al" "Waiter" "Mrs Mak" y hasNext() y next() it1 public class SymTabKeyIterator { … public boolean hasNext() { … } public String next() { … } } Page 6 of 18 SymbolTable_v1...
View Full Document

Page1 / 18

L6_Iterator - Iterators In this lecture we shall take a...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online