1 / 16

Using Collections

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 {

tieve
Download Presentation

Using Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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<String> 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 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.

  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 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

  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 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()); } }

  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 type wrapper class int Integer float Float char Character ... ...

  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<Integer> markList; … public void storeMark(int mark) { markList.add(mark); } autoboxing int firstMark = markList.remove(0); 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)

More Related