Midterm Review1.8

Midterm Review1.8 - Iterators An Iterator is an object that...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Iterators An Iterator is an object that enables you to traverse through a collection and to remove elements from the collection selectively, if desired. You get an Iterator for a collection by calling its iterator method. Suppose collection is an instance of a Collection. Then to print out each element on a separate line: Iterator<E> it = collection.iterator(); while (it.hasNext()) System.out.println(it.next()); CSE 2011 Prof. J. Elder - 36 - Last Updated: 1/7/10 10:14 AM The Enhanced For-Each Statement Suppose collection is an instance of a Collection. Then for (Object o : collection) System.out.println(o); prints each element of the collection on a separate line. This code is just shorthand: it compiles to use o.iterator(). CSE 2011 Prof. J. Elder - 37 - Last Updated: 1/7/10 10:14 AM The Java Collections Framework (Ordered Data Types) Iterable Interface Abstract Class Collection Class List Abstract Collection Queue Abstract List Abstract Queue Priority Queue Abstract Sequential List Array List Vector Stack Linked List CSE 2011 Prof. J. Elder - 38 - Last Updated: 1/7/10 10:14 AM Recursion Chapter 3.5 CSE 2011 Prof. J. Elder - 39 - Last Updated: 1/7/10 10:14 AM Linear Recursion Design Pattern Test for base cases Begin by testing for a set of base cases (there should be at least one). Every possible chain of recursive calls must eventually reach a base case, and the handling of each base case should not use recursion. Recurse once Perform a single recursive call. (This recursive step may involve a test that decides which of several possible recursive calls to make, but it should ultimately choose to make just one of these calls each time we perform this step.) Define each possible recursive call so that it makes progress towards a base case. CSE 2011 Prof. J. Elder - 40 - Last Updated: 1/7/10 10:14 AM ...
View Full Document

This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.

Ask a homework question - tutors are online