Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.

Similar presentations


Presentation on theme: "Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0."— Presentation transcript:

1 Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0

2 Main concepts to be covered Collections (especially ArrayList) Modul 3

3 The requirement to group objects Many applications involve collections of objects: Personal organizers. Library catalogs. Student-record system. The number of items to be stored varies. Items added. Items deleted. Modul 3

4 A personal notebook Notes may be stored. Individual notes can be viewed. There is no limit to the number of notes. It will tell how many notes are stored. Explore the notebook1 project. Modul 3

5 Take a quick look at notebook1 Explore the notebook1 project. Modul 35

6 Class libraries Collections of useful classes. We don’t have to write everything from scratch. Java calls its libraries, packages. Grouping objects is a recurring requirement. The java.util package contains classes for doing this. Modul 3

7 Module 3 import java.util.ArrayList; /** *... */ public class Notebook { // Storage for an arbitrary number of notes. private ArrayList notes; /** * Perform any initialization required for the * notebook. */ public Notebook() { notes = new ArrayList (); }... } Importing Class libraries

8 Collections We specify: the type of collection: ArrayList the type of objects it will contain: We say, “ArrayList of String”. Modul 3

9 Object structures with collections Modul 3

10 Adding a third note Modul 3

11 Features of the collection 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. Does that matter? Does not knowing how prevent us from using it? Modul 3

12 Using the collection Modul 3 public class Notebook { private ArrayList notes;... public void storeNote(String note) { notes.add(note); } public int numberOfNotes() { return notes.size(); }... } Adding a new note Returning the number of notes (delegation)

13 Index numbering Modul 3

14 Retrieving an object Modul 3 Index validity checks public void showNote(int noteNumber) { if(noteNumber < 0) { // This is not a valid note number. } else if(noteNumber < numberOfNotes()) { System.out.println(notes.get(noteNumber)); } else { // This is not a valid note number. } Retrieve and print the note

15 Removal may affect numbering Modul 3

16 Generic classes Collections are known as parameterized or generic types. ArrayList implements list functionality: add, get, size, etc. The type parameter says what we want a list of: ArrayList etc. Modul 3

17 Review Collections allow an arbitrary number of objects to be stored. Class libraries usually contain tried-and-tested collection classes. Java’s class libraries are called packages. We have used the ArrayList class from the java.util package. Modul 3

18 Review 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. Modul 3

19 Some Collections types List Set SortedSet NavigableSet Map Queue Modul 319 java.util.ArrayList java.util.LinkedList java.util.Vector java.util.Stack java.util.EnumSet java.util.HashSet java.util.LinkedHashSet java.util.TreeSet java.util.SortedSet java.util.NavigableSet java.util.HashMap java.util.Hashtable java.util.EnumMap java.util.IdentityHashMap java.util.LinkedHashMap java.util.Properties java.util.TreeMap java.util.WeakHashMap java.util.LinkedList java.util.PriorityQueue

20 Grouping objects Collections and the for-each loop

21 Interlude: Some popular errors... Modul 3 Først en kvik lille test

22 /** * Print out notebook info (number of entries). */ public void showStatus() { if(notes.size() == 0); { System.out.println(“Notebook is empty”); } else { System.out.print(“Notebook holds “); System.out.println(notes.size() + “ notes); } } Modul 3 What’s wrong here?

23 /** * Print out notebook info (number of entries). */ public void showStatus() { if(notes.size() == 0); { System.out.println(“Notebook is empty”); } else { System.out.print(“Notebook holds “); System.out.println(notes.size() + “ notes); } } Modul 3 This is the same as before!

24 /** * Print out notebook info (number of entries). */ public void showStatus() { if(notes.size() == 0) ; { System.out.println(“Notebook is empty”); } else { System.out.print(“Notebook holds “); System.out.println(notes.size() + “ notes); } } Modul 3 This is the same again

25 /** * Print out notebook info (number of entries). */ public void showStatus() { if(notes.size() == 0) { ; } { System.out.println(“Notebook is empty”); } else { System.out.print(“Notebook holds “); System.out.println(notes.size() + “ notes); } } Modul 3 and the same again…

26 /** * Print out notebook info (number of entries). */ public void showStatus() { if(isEmpty = true) { System.out.println(“Notebook is empty”); } else { System.out.print(“Notebook holds “); System.out.println(notes.size() + “ notes); } } Modul 3 This time I have a boolean field called ‘isEmpty’... What’s wrong here?

27 /** * Print out notebook info (number of entries). */ public void showStatus() { if(isEmpty == true) { System.out.println(“Notebook is empty”); } else { System.out.print(“Notebook holds “); System.out.println(notes.size() + “ notes); } } Modul 3 This time I have a boolean field called ‘isEmpty’... The correct version

28 /** * Store a new note in the notebook. If the * notebook is full, save it and start a new one. */ public void addNote(String note) { if(notes.size() == 100) notes.save(); // starting new notebook notes = new ArrayList (); notes.add(note); } Modul 3 What’s wrong here?

29 /** * Store a new note in the notebook. If the * notebook is full, save it and start a new one. */ public void addNote(String note) { if(notes.size == 100) notes.save(); // starting new notebook notes = new ArrayList (); notes.add(note); } Modul 3 This is the same.

30 /** * Store a new note in the notebook. If the * notebook is full, save it and start a new one. */ public void addNote(String note) { if(notes.size == 100) { notes.save(); // starting new notebook notes = new ArrayList (); } notes.add(note); } Modul 3 The correct version

31 Main concepts to be covered Collections Loops: the for-each loop Modul 3

32 Iteration We often want to perform some actions an arbitrary number of times. E.g., print all the notes in the notebook. How many are there? Most programming languages include loop statements to make this possible. Java has several sorts of loop statement. We will start with its for-each loop. Modul 3

33 Iteration fundamentals We often want to repeat some actions over and over. Loops provide us with a way to control how many times we repeat those actions. With collections, we often want to repeat things once for every object in a particular collection. Modul 3

34 For-each loop pseudo code Modul 3 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

35 A Java example Modul 3 /** * 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

36 Review Loop statements allow a block of statements to be repeated. The for-each loop allows iteration over a whole collection. Modul 3

37 Grouping objects The while loop

38 Main concepts to be covered The while loop Moduil 3

39 The while loop A for-each loop repeats the loop body for each object in a collection. Sometimes we require more variation than this. We can use a boolean condition to decide whether or not to keep going. A while loop provides this control. Modul 3

40 While loop pseudo code Modul 3 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

41 A Java example Modul 3 /** * List all notes in the notebook. */ public void listNotes() { int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; } Increment index by 1 while the value of index is less than the size of the collection, print the next note, and then increment index

42 for-each versus while for-each: easier to write. safer: it is guaranteed to stop. while: we don’t have to process the whole collection. doesn’t even have to be used with a collection. take care: could be an infinite loop. Modul 3

43 While without a collection Modul 3 // Print all even numbers from 0 to 30. int index = 0; while(index <= 30) { System.out.println(index); index = index + 2; }

44 Searching a collection Modul 3 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.

45 Grouping objects Iterators

46 Main concepts to be covered Iterators Modul 3

47 :Element myList:List :Element :Iterator myList.iterator() :Element

48 Modul 3 :Element :Iterator hasNext()? ✔ next() Element e = iterator.next(); :Element :Iterator myList:List

49 Modul 3 :Element hasNext()? ✔ next() :Element :Iterator myList:List

50 Modul 3 :Element hasNext()? ✔ next() :Element :Iterator myList:List

51 Modul 3 :Element hasNext()? ✔ next() :Element :Iterator myList:List

52 Modul 3 :Element hasNext()? ✗ :Element :Iterator myList:List

53 Using an Iterator object Modul 3 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()); }

54 Index versus Iterator Ways to iterate over a collection: for-each loop. Use if we want to process every element. while loop. Use if we might want to stop part way through. Use for repetition that doesn't involve a collection. Iterator object. Use if we might want to stop part way through. Often used with collections where indexed access is not very efficient, or impossible. Iteration is an important programming pattern. Modul 3

55 Exercises On paper 4.2, 4.3, 4.4, 4.5, 4.6, 4.10 and 4.11 Notebook 4.10 – 4.16 Bank Solve Modul3Bank found under Opgaver/Modul3 Extra ExercisesOnLoops SportlExercise Modul 3

56 Exercise On paper 4.2, 4.3, 4.4, 4.5, 4.6 on page 93 to Modul 3


Download ppt "Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0."

Similar presentations


Ads by Google