Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright by Scott GrissomCh 4 Grouping Objects Slide 1 Flexible-size Collections flexible collection of objects unlimited and unknown size iPod playlist.

Similar presentations


Presentation on theme: "Copyright by Scott GrissomCh 4 Grouping Objects Slide 1 Flexible-size Collections flexible collection of objects unlimited and unknown size iPod playlist."— Presentation transcript:

1 copyright by Scott GrissomCh 4 Grouping Objects Slide 1 Flexible-size Collections flexible collection of objects unlimited and unknown size iPod playlist as an example ArrayList provided in java.util.*; ArrayList playList; playList = new ArrayList (); Methods add(Object) // to end of list int size () Object get (int index) Object remove (int index) first item is in location zero Draw object diagram

2 copyright by Scott GrissomCh 4 Grouping Objects Slide 2 Processing Elements Cumbersome playList.get(0).play(); playList.get(1).play(); playList.get(2).play(); We need a flexible way to access each element even though we do not know how many For-each loop process each item in order until done for (Song s : playList){ s.play(); }

3 copyright by Scott GrissomCh 4 Grouping Objects Slide 3 Group Activity ArrayList stuff; Stuff = new ArrayList (); Stuff.add(“Howdy!”); Stuff.add(“My Name “); stuff.add(“is “); stuff.add(“Ralph”); stuff.remove(2); for(String str : stuff){ System.out.println(str); }

4 copyright by Scott GrissomCh 4 Grouping Objects Slide 4 While loop an alternative repetition structure int x = 0; while (x < 10){ System.out.println(x); } Advantages no collection required every element does not have to be processed Example Notebook search (p 94) Group exercises 4.16 4.17 4.18

5 copyright by Scott GrissomCh 4 Grouping Objects Slide 5 Auction System (4.10) Multiple classes Person contains a name Bid contains a value and Person Lot a description, lot #, high bid review the bidFor method Auction page 99 an ArrayList of logs and nextLotNumber review the bidFor method Group activities write additional methods 4.26 close 4.27 getUnsold() 4.30 removeLot()

6 copyright by Scott GrissomCh 4 Grouping Objects Slide 6 Fixed-size Collections called an array able to store objects or primitive types first element at location 0 uses a different syntax than ArrayList int [ ] nums; nums = new int [10]; used just as a variable nums[3] = 100; x = 5; nums[x] = 7; value = nums[x] + 8;

7 copyright by Scott GrissomCh 4 Grouping Objects Slide 7 for loop different syntax than for-each three parts for (int i = 0; i < 20; i++) System.out.println(i); for(index = 0; index < array.length; index++) total = total + array[index]; Group Activities sum average max min find

8 copyright by Scott GrissomCh 4 Grouping Objects Slide 8 Which loop? most are interchangeable while number of iterations is determined during performance for number of iterations is know before loop begins for-each process all elements of a collection


Download ppt "Copyright by Scott GrissomCh 4 Grouping Objects Slide 1 Flexible-size Collections flexible collection of objects unlimited and unknown size iPod playlist."

Similar presentations


Ads by Google