Comp 170, Week 4 (Week of Feb 02) pld Notebook lab-classes picdemo.Numerics exam 1 is Feb 18, up through the end of chapter 4. ============================================================ ============================================================ for:each loop versus while loop: int totallen = 0; // in the file for(String note : notes) { totallen += note.length(); } While loop: int totallen = 0; int n = 0; while (n it = notes.iterator(); while(it.hasNext()) { totallen += it.next().length(); } MailServer iterator loop: Use iterator (or while) because we can remove an item from the list being processed. public MailItem getNextMailItem1(String who) { Iterator it = items.iterator(); while(it.hasNext()) { MailItem item = it.next(); if(item.getTo().equals(who)) { it.remove(); return item; } } return null; } ================================================= loop patterns: processing a list processing a range of numbers 1..N processing N times printing or other repeated action sum search max ================================================================ Monday's lab: Kakutani/Collatz sequences Start with an integer N > 1. if N is odd, let N = 3*N+1 if N is even, let N = N/2 keep going. 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 Do you ever reach 1? (apparently so, though it's not proven) If so, how long does it take? What is the largest N reached on the way?