160 likes | 276 Vues
Using Collections. 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. Example from book. public class Notebook {
E N D
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.
Example from book public class Notebook { private ArrayList<String> notes; ... public void storeNote(String note) { notes.add(note); } public int numberOfNotes() { return notes.size(); } ... }
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.
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?
For-each loop pseudo code General form of the for-each loop forkeyword loop header for(ElementType element : collection) { loop body } Statement(s) to be repeated Pseudo-code expression of the actions of a for-each loop For each element in collection, do the things in the loop body.
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
While loop pseudo code General form of a while loop whilekeyword boolean test while(loop condition) { loop body } Statements to be repeated Pseudo-code expression of the actions of a while loop while we wish to continue, do the things in the loop body
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.
Using an Iterator object returns an Iterator object java.util.Iterator Iterator<ElementType> it = myCollection.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object } public void listNotes() { Iterator<String> it = notes.iterator(); while(it.hasNext()) { System.out.println(it.next()); } }
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
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?
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 type wrapper class int Integer float Float char Character ... ...
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.
Autoboxing and unboxing private ArrayList<Integer> markList; … public void storeMark(int mark) { markList.add(mark); } autoboxing int firstMark = markList.remove(0); unboxing
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)