Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.

Similar presentations


Presentation on theme: "Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps."— Presentation transcript:

1 Using Collections

2 Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps the objects in order. Details of how all this is done are hidden.

3 Example from book public class Notebook { private ArrayList notes;... public void storeNote(String note) { notes.add(note); } public int numberOfNotes() { return notes.size(); }... }

4 Items may be added and removed. Each item has an index. Index values may change if items are removed (or further items added). The main ArrayList methods are add, get, remove and size. ArrayList is a parameterized or generic type.

5 Exercises Define the fields Player, Club and League class using collections The Match class records who played and who scored. What fields would this class have?

6 For-each loop pseudo code for(ElementType element : collection) { loop body } For each element in collection, do the things in the loop body. loop header for keyword Statement(s) to be repeated Pseudo-code expression of the actions of a for-each loop General form of the for-each loop

7 A Java example /** * List all notes in the notebook. */ public void listNotes() { for(String note : notes) { System.out.println(note); } for each note in notes, print out note

8 While loop pseudo code while(loop condition) { loop body } while we wish to continue, do the things in the loop body boolean test while keyword Statements to be repeated Pseudo-code expression of the actions of a while loop General form of a while loop

9 Searching a collection int index = 0; boolean found = false; while(index < notes.size() && !found) { String note = notes.get(index); if(note.contains(searchString)) { // We don't need to keep looking. found = true; } else { index++; } // Either we found it, or we searched the whole // collection.

10 Using an Iterator object Iterator it = myCollection.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object } java.util.Iterator returns an Iterator object public void listNotes() { Iterator it = notes.iterator(); while(it.hasNext()) { System.out.println(it.next()); }

11 Exercises For the Club class write “print squad” method “squad average height” method “top scorer” method For the League class write “print clubs” method “top scorer” method

12 Collections and primitive types All objects can be entered into collections...... because collections accept elements of type Object...... and all classes are subtypes of Object. Great! But what about simple types?

13 Wrapper classes Primitive types (int, char, etc) are not objects. They must be wrapped into an object! Wrapper classes exist for all simple types: simple typewrapper class intInteger floatFloat charCharacter...

14 Wrapper classes int i = 18; Integer iwrap = new Integer(i); … int value = iwrap.intValue(); wrap the value unwrap it In practice, autoboxing and unboxing mean we don't often have to do this.

15 Autoboxing and unboxing private ArrayList markList; … public void storeMark(int mark) { markList.add(mark); } int firstMark = markList.remove(0); autoboxing unboxing

16 Exercises A BenchTest class holds a set of marks. Write methods to: Find the highest score Find the average score Find the standard deviation sqrt(sum((value-mean)^2)) Find the median (score achieved by mid- ranking student) Find the mode (most freqeuent score)


Download ppt "Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps."

Similar presentations


Ads by Google