Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal

Similar presentations


Presentation on theme: "Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal"— Presentation transcript:

1 Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal emreilal@iyte.edu.tr

2 Recap Assignment 08 File input/output

3 Today Assignment 9 Data structures Thinking in Java – Chapter 11

4 Collections We need to give a meaningful structure to the data Array is a basic structure We mentioned java.util.Vector Some common structes exist that programmers often make use of. Each is an analogy for a real world situation. Örnekler: –Bag –Heap –Set –List –Queue –Table –Tree

5 java.util.* Java 2 "Collections" – A "framework" (iskelet) –Interfaces (Kontratlar) Collection, Set, Map, etc. –Implementations (Uygulamaları) TreeMap, LinkedList, ArrayList, etc. –Older classes that existed in Java 1.1 Vector, Hashtable –Helper classes Arrays, Iterator, etc. –Algorithms Sort, etc.

6 Interfaces (Kontratlar) Defines the type of data structure –3 Main Types Set: No duplicate elements allowed List: Can have duplicates, order is important Map: Elements are recalled based on an index

7 Interfaces (Kontratlar) 6 interfaces in total –Collection (general groupings) Set(elements are important – can be ordered) List(order is the determining factor) SortedSet(a set that is kept in order based on a comparing method. [Comparator]) –Map (matching of an element to a unique ‘key’ for later retrieval) SortedMap(collection that is ordered by the key)

8 Classes Implementation Type HashTable Variable sized Array Balanced Tree Linked list Interface Set HashSetTreeSet List ArrayListLinkedList Map HashMapTreeMap

9 Set Classes HashSet –A structure that stores elements using a hashtable algorithm. Fastest set. TreeSet –A sorted set implementation using a "Red-Black" sorting algorithm.

10 Examples - Sets import java.util.*; HashSet toolBox = new HashSet(); toolBox.add(“hammer"); toolBox.add(“screwdriver"); toolBox.add(“pliers"); if ( !toolBox.isEmpty() ) println( toolBox.size() ); println ( toolBox );

11 List Classes ArrayList –Similar to a Vector. An automatically resizable Array implementation. LinkedList –“Doubly linked list” implementation where given an element you can access the previous and next elements. Useful in modelling queues.

12 Example - Lists import java.util.*; ArrayList attendance = new ArrayList(); attendance.add("Ali Durmaz"); attendance.add("Veli Kaymaz"); attendance.add("Ayşe Kaçmaz"); for (i=0; i< attendance.size(); i++) println(attendance.get(i) );

13 Map Classes HashMap –An implementation where keys are stored and retrieved using a hastable. Fast. TreeMap –A SortedMap implementation that keeps keys ordered using a "Red-Black" sorting algorithm.

14 Example - Maps import java.util.*; HashMap team = new HashMap(); team.put(“defender", “Emre"); team.put(“goalie", “Kadir"); team.put(“striker", “Lale"); if ( ! team.containsKey(“midfielder") ) team.put(“midfielder", “Mustafa");

15 Helper classes Arrays –Static methods Searching, sorting, comparison, populating, etc. Basic usage –Iterator –Comparator –Exceptions

16 Iterator Mechanism that allows us to examine the elements stored in a data structure one by one. –The iterator() that exists in all collections returns an iterator that we can use. –Iterator only has three methods Object next() boolean has next() void remove() ListIterator –Has additional methods that are suitable for lists. int nextIndex() void set(Object o) boolean hasPrevious() …

17 Example - Iterator import java.util.*; Iterator i = attendance.iterator(); while ( i.hasNext() ) System.out.println( i.next() );

18 Generics – Java 1.5 Previously a collection had no idea what Type (the class they belonged to) of elements it was storing. Java 1.5 provides “generics”. Through this mechanism, the compiler guarantees that the program uses the correct classes.

19 Generics – example ArrayList attendance = new ArrayList (); attendance.add("Ali Durmaz"); attendance.add("Veli Kaymaz"); attendance.add(new Integer(25)); //error!! for (i=0; i< attendance.size(); i++) println(attendance.get(i) ); Iterator i = attendance.iterator(); while ( i.hasNext() ) System.out.println( i.next() );

20 Assignment 10 Rewrite the ShapeApplicaitons you have developed for assignment 07, using Collections. –Choose a collection for Shapes –Choose a group for colors –etc. Hint: Make use of Comparator to simplify sorting...

21 Next week Interfaces and beyond


Download ppt "Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal"

Similar presentations


Ads by Google