Week 5: Week of Sept 24 Explanation of Lab 3 Collatz sequences 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 N=15: max=160 N=25: len=23 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 N=27: Max = 9232, len=111 classic-for loops: know # of iterations ahead of time for:each loop: # of iterations is the length of the list! ========================================================= Arrays Allocating arrays v allocating array elements classic for-loops: initializing an array of ints to 0 adding the elements of an array finding the max of an array counting selected elements of an array classic for-loops v for:each loops was the classic for-loop really *designed* for arrays? for:each loop: no access to the numeric position in the list! classic-for / while equivalence for:each and arrays =================================================== Weblog-analyzer overview of program input: how much do we really have to know? how would we do it with an ArrayList? Adding Weekly counts ======================================================================== Wednesday: chart version, fixed (rectangles grow downwards!) arrays v ArrayList: get/set interface is awkward. For many lists, though,we NEVER use it: insert using .add(item) access using for:each loops How new Foo(...) works memory view of object creation and of arrays ArrayList expansion, NumList ======================================================================= Friday: Example demoing how "real" ArrayLists handle growth Sizes: 10, 16, 25, 38, 58, 88, 133, 200 next size = floor(1.5*prev_size) + 1; Hard loops: loops that MODIFY the list structure. Example: in the MailSystem, getNextMessage(user) found the next message for the user, DELETED IT from the master list, and returned it. However, returning a new list is NOT so hard: notes: ArrayList --------- matches = new ArrayList(); for (String note : notes) { if (note.contains(str)) { matches.add(note); } } My own NumList using it in the program