Top Banner
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal [email protected]
21

Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal [email protected].

Jan 02, 2016

Download

Documents

Jerome Blake
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal emreilal@iyte.edu.tr.

Object Oriented Programming

Ders 10: Data Structures

Mustafa Emre İ[email protected]

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

Recap

• Assignment 08

• File input/output

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

Today

• Assignment 9

• Data structures

• Thinking in Java – Chapter 11

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

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

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

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.

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

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

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

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)

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

Classes

Implementation Type

HashTableVariable sized

ArrayBalanced

TreeLinked list

Interface

Set HashSet TreeSet

List ArrayList LinkedList

Map HashMap TreeMap

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

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.

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

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

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

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.

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

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

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

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.

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

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");

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

Helper classes

• Arrays– Static methods

• Searching, sorting, comparison, populating, etc.

• Basic usage– Iterator

– Comparator

– Exceptions

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

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()• …

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

Example - Iterator

import java.util.*;

Iterator i = attendance.iterator();

while ( i.hasNext() )

System.out.println( i.next() );

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

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.

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

Generics – example

ArrayList<String> attendance = new ArrayList<String>();

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<String> i = attendance.iterator();

while ( i.hasNext() )

System.out.println( i.next() );

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

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

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

Next week

• Interfaces and beyond