001package algs13; 002 003import java.util.Iterator; 004import stdlib.*; 005 006public class XIteratorClient { 007 public static int sum1 (Stack<Integer> a) { 008 int result = 0; 009 Iterator<Integer> it = a.iterator (); 010 while (it.hasNext ()) { 011 int item = it.next (); 012 result += item; 013 } 014 return result; 015 } 016 public static int sum2 (Stack<Integer> a) { 017 int result = 0; 018 for (int item : a) { 019 result += item; 020 } 021 return result; 022 } 023 public static void main (String[] args) { 024 Stack<Integer> a = new Stack<> (); 025 a.push (-1); 026 a.push (-2); 027 a.push (-3); 028 StdOut.println (sum1(a)); 029 030 } 031 032}