Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Session 4 Java Collections. Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The.

Similar presentations


Presentation on theme: "Practical Session 4 Java Collections. Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The."— Presentation transcript:

1 Practical Session 4 Java Collections

2 Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The Collections class Wrapper classes

3 Collection A group of elements. The group size can be changed during run-time. A collection object has many useful methods for manipulating the collection: Inserting elements Deleting elements Copying the collection Iterating over the elements Computing subsets of elements … Various types of collections are defined in the standard library: Vector, ArrayList, Stack, ArrayDequeue, PriorityQueue, TreeSet,…

4 Collection Why shouldn’t we just use arrays? Arrays are not objects! You cannot invoke methods on arrays. Arrays have fixed size. Arrays are less convenient for representing certain types of collections: Sets Double-ended queues Dictionaries …

5 Working with a collection import java.util.Vector; … Vector carsVec = new Vector (); Car Volvo = new Car(2.0); Car BMW = new Car(2.2); carsVec.add(Volvo); carsVec.add(BMW); for (Car c: carsVec) System.out.println(c); Definition syntax: Collection colName = new Collection () Example:

6 Working with a collection Car temp = carsVec.elementAt(0); carsVec.set(0,carsVec.elementAt(1)); carsVec.set(1, temp); for (int i=0; i < carsVec.size(); i++) System.out.println(carsVec.elementAt(i)); Example - continued:

7 The Collection Interface Collection is an interface in the standard java library. The Collection interface is generic. It accepts a type as a parameter. public interface Collection extends Iterable { int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator iterator(); Object[] toArray(); T[] toArray(T[] a); …

8 The Collection Hierarchy Collection ListSetQueue (Partial illustration)

9 The Collection Hierarchy Collection ListSetQueue Vector (Partial illustration)

10 The Collection Hierarchy Collection ListSetQueue Vector Stack ArrayListPriorityQueue (Partial illustration)

11 Case Study: Undoable Stack Various programs allow the user to undo his operations. The undo operations are performed in reverse order. In such a program, we need to add each operation with its parameters onto the stack.

12 Program Stack

13 Program Stack Resize 36

14 Program Stack Resize 36 Recolor 4

15 Program Stack Resize 36 Recolor 4 Resize 24 Undo: Resize(36)

16 Program Stack Resize 36 Recolor 4 Resize 24 Resize 36 Recolor 4 Undo: Resize(36)

17 Undoable Stack Hierarchy

18 TextArea -text: String -size: int -color: int + getters… + setters…

19 Undoable Stack Hierarchy Operation + perform(arg: int) + getArg() : int TextArea -text: String -size: int -color: int + getters… + setters…

20 Undoable Stack Hierarchy Operation + perform(arg: int) + getArg() : int Recolor - color: int + perform(arg: int) + getArg() : int Resize - size: int + perform(arg: int) + getArg() : int TextArea -text: String -size: int -color: int + getters… + setters…

21 Undoable Stack Hierarchy Operation + perform(arg: int) + getArg() : int Recolor - color: int + perform(arg: int) + getArg() : int Resize - size: int + perform(arg: int) + getArg() : int TextArea -text: String -size: int -color: int + getters… + setters… UndoStack + add (op :Operation) + undo() 0..n

22 The Collections class The Collections class contains static methods that operate on collections: min max reverse sort … Example: import java.util.Collections; import java.util.Vector; … Vector carsVec = new Vector (); … Collections.reverse(carsVec);

23 Wrapper Classes What happens if we want to create a collection of a primitive type? Collections can be made only of objects. Primitive types are not objects. We use wrapper classes instead. A wrapper class is an object representing a primitive type.

24 Wrapper Classes int float double char boolean Integer Float Double Character Boolean Example: Integer intObj = new Integer(10); int number = intObj.intValue();

25 A collection of wrapper objects Import java.util.Vector; … Vector numVec = new Vector (); int size = 10; for (int i = size; i >0; i--) numVec.add(new Integer( i )); Collections.sort(numVec); for (Integer i: numVec) System.out.println( i);


Download ppt "Practical Session 4 Java Collections. Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The."

Similar presentations


Ads by Google