Object Oriented Software Development 6. Arrays and collections.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Generics, Lists, Interfaces
Chapter 9: Data Structures I
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
TCSS 342, Winter 2005 Lecture Notes
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Stacks, Queues, and Deques
Building Java Programs
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Data structures Abstract data types Java classes for Data structures and ADTs.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Holding Your Objects Generics and type-safe containers One of the problems of using pre-Java SE5 containers was that the compiler allowed.
Object Oriented Software Development
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Understanding Data Types and Collections Lesson 2.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Computer Science Projects Internal Assessment. Mastery Item Claimed Justification Where Listed Random Access File – Searching Lines P. 53 Random.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Linear Data Structures
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Java Collections Framework The client view. The Big Picture.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Understanding Data Types and Collections Lesson 2.
Understanding Algorithms and Data Structures
structures and their relationships." - Linus Torvalds
List Data Structure.
Dynamic Data Structures and Generics
Introduction to Data Structure
Fundaments of Game Design
structures and their relationships." - Linus Torvalds
Presentation transcript:

Object Oriented Software Development 6. Arrays and collections

One-to-many relationships A department can have many employees in it One-to-many “has-a” relationship “has-a” relationship is implemented with instance variable This variable must be able to hold a collection of Employee objects Object Oriented Software Development 6. Arrays and collections 2

Collections of data Most software applications need to work with collections of similar objects, for example: List of transactions to display in a bank application List of sprites in a game Often need to perform actions such as: Listing all the objects in a collection Adding and removing objects Finding a specific object in a collection Object Oriented Software Development 6. Arrays and collections 3

Arrays Simple way of storing collection of objects of the same type Contents can be any type, including reference types and value types Arrays are stored on the heap If content type is a reference type, the array stores references to objects Object Oriented Software Development 6. Arrays and collections 4

Looping through an array for and foreach loops: foreach loop works with any collection which implements an interface called IEnumerable We will look at this again later Object Oriented Software Development 6. Arrays and collections 5

Arrays and polymorphism An array can store instances of its declared type or subclasses of that type May need to cast to get object back out as its original type Object Oriented Software Development 6. Arrays and collections 6

More than one dimension Arrays can have two dimensions Can have more than two dimensions Can be rectangular or jagged Jagged array = array of arrays Arrays may be different sizes Object Oriented Software Development 6. Arrays and collections 7

Limitations of arrays Once created, the size of an array is fixed Array does not provide ways of adding, removing or finding elements To use an array you need to write code to access elements Often better to use classes which are designed to store data and provide methods for accessing the data Data structures, or collections Object Oriented Software Development 6. Arrays and collections 8

Abstract data types An ADT is a specification for the set of operations which can be performed on the data stored in a data structure “Abstract” because the specification is independent of the concrete implementation Could have many implementations of the same ADT which work differently “under the hood” We will look first at the list ADT and two ways of implementing it Object Oriented Software Development 6. Arrays and collections 9

List A list is a sequential data structure Addition and removals can be made at any position in the list Lists are normally in the form of a 1,a 2,a a n. The size of this list is n. The first element of the list is a 1,and the last element is a n The position of element a i in a list is i. Object Oriented Software Development 6. Arrays and collections 10

List operations A list data structure should be able to provide the following operations: Create a list Find an item in the list Insert an item in the list Modify an item in the list Delete an item from the list Check whether list is full/empty Traverse the list Object Oriented Software Development 6. Arrays and collections 11

Lists example CollectionsDemo solution CollectionsDemo and SimpleCollections projects Data structures in separate project from program – can easily be reused by other programs SimpleCollections is a class library project CollectionsDemo – Program.cs SimpleCollections – ISimpleList.cs, SimpleArrayList.cs, SimpleLinkedList.cs Object Oriented Software Development 6. Arrays and collections 12

Implementing a list We can express these requirements as an interface Lists will implement this interface Object Oriented Software Development 6. Arrays and collections 13

Data type of items Note that in interface code, the item type is Object Polymorphism – each list item can be: An instance of Object An instance of any type which derives from object......which is essentially any type at all as all types in.NET derive from Object Reusable – can be used to store any type of items in any application Object Oriented Software Development 6. Arrays and collections 14

Implementation details There are two common ways of actually storing the data in a list ArrayList Items are stored in an array LinkedList Items are stored as a chain of connected nodes Each node has a data item and a reference to the next node Object Oriented Software Development 6. Arrays and collections 15

Does it matter which one? Both versions can be written to implement the interface, and both will work perfectly well In fact, we don’t even need to write our own implementations as the.NET Framework library already has a wide selection of tried and test collection classes, including lists But, different implementations have different performance in particular situations Need to understand characteristics of structures to make the best choice Object Oriented Software Development 6. Arrays and collections 16

Simple implementations We will look at the code for simplified implementations of ArrayList and LinkedList This will help us to understand how the two versions work and identify where there might be performance differences Wouldn’t use these in real programs – Framework versions are more complex, but more capable and robust Use Framework classes unless you need specific custom functionality Object Oriented Software Development 6. Arrays and collections 17

SimpleArrayList Items stored in array of type Object Capacity is the number of items which can be stored Size is the number of items currently stored Object Oriented Software Development 6. Arrays and collections 18 instance variables constructor

Adding items to SimpleArrayList Adding a new item at the end of the list is simple Number of items stored so far = size Index of last stored = size - 1 Store the new item at index = size Object Oriented Software Development 6. Arrays and collections 19

Adding items to SimpleArrayList Adding a new item at a specified index involves more work Need to shuffle items up to make space Object Oriented Software Development 6. Arrays and collections 20

Adding items to SimpleArrayList Start at first empty position (size) Copy previous entry into current one Move down one position Copy previous entry into current one Repeat until target position reached Put new item in target position Object Oriented Software Development 6. Arrays and collections 21

SimpleLinkedList Each item is stored in an object of type Node Contains item and reference to next Node List only needs reference to first Node, can find others from there by following references No capacity limit, list can grow Object Oriented Software Development 6. Arrays and collections 22 Node class constructor reference to head node, initially set to null by constructor

Adding items to SimpleLinkedList To add a new item at a specified index break chain at target position and insert new node Object Oriented Software Development 6. Arrays and collections 23 new item to be added at index 2 probe follows chain to find target node new item inserted

Adding items to SimpleLinkedList Special case when inserting first item in empty list Simply set head to point to new node Adding at end is not a particularly special case Need to probe all the way along the list to find last item then insert new node Object Oriented Software Development 6. Arrays and collections 24 head of list, initially null

Adding items to SimpleLinkedList Special case when inserting at start of list Object Oriented Software Development 6. Arrays and collections 25

Adding items to SimpleLinkedList Implementation for general case Object Oriented Software Development 6. Arrays and collections 26

Other operations How would deleting be implemented in each case? How would searching be implemented? How would modifying be implemented? Look at sample code to see how other operations are implemented Object Oriented Software Development 6. Arrays and collections 27

Which list should we use? Need to judge likely effect of list characteristics on performance Real applications may store a large number of data Consider the way the data will be added and accessed Will new items be added frequently? Where in the list will they be added? Will we need to access items frequently? Where in the list will we need to access items? Object Oriented Software Development 6. Arrays and collections 28

ArrayList Time to access does not depend on the size of the list To add an element at the end of the list, the time taken does not depend on the size Time taken to add an element at any other point in the list does depend on the size Additions near the start of the list take longer than additions near the middle or end Deletes near the start of the list take longer Object Oriented Software Development 6. Arrays and collections 29

LinkedList Time to access an element depends on the index because each element of the list must be traversed until the required index is found Time to add an element does not depend on the size of the list, as no shifts are required Time to add does depend on the index Additions near the end of the list take longer than additions near the middle or start Deletes near the end of the list take longer Object Oriented Software Development 6. Arrays and collections 30

Variations on LinkedList Circularly linked list The tail of the list always points to the head Can start anywhere and reach whole list Doubly linked list Permits searching in both directions Object Oriented Software Development 6. Arrays and collections 31

Capacity of lists LinkedList is dynamic No limit on capacity ArrayList is static Capacity defined by length of underlying array Could be a big disadvantage, but....NET Framework implementation of ArrayList can resize itself dynamically When array gets close to being full the ArrayList creates a new, larger array and copies items into it Object Oriented Software Development 6. Arrays and collections 32

Traversing a collection Traversing means passing through the collection visiting each item in turn The details of how this works depend on the way the collection is implemented Traversing an ArrayList simply involves passing along the array Traversing a LinkedList involves following references to reach each node Collection should provide a way of traversing Object Oriented Software Development 6. Arrays and collections 33

foreach loop and IEnumerable In C# the foreach-in loop is the usual way to traverse a collection Collection needs to provide help for the foreach loop to work Collection must implement IEnumerable interface Object Oriented Software Development 6. Arrays and collections 34

IEnumerable interface Defines a contract If a collection type implements IEnumerable interface then it agrees to provide the mechanism which can enumerate, or loop though, the collection Defines one method – GetEnumerator, which returns an instance of a class which in turn implements IEnumerator Most.NET collection types and arrays implement this interface Object Oriented Software Development 6. Arrays and collections 35

Enumerators The IEnumerator interface defines an enumerator object which traverses a collection in a way specific to that collection Enumerator object can be used by foreach loop or can be used directly in your code Defines: MoveNext method Reset method Current property See example in lab exercises Object Oriented Software Development 6. Arrays and collections 36

More data structures Lists are suitable for many applications However, there are other data structures which have characteristics which make them more suited to specific applications, including: Stack Queue Dictionary Object Oriented Software Development 6. Arrays and collections 37

Stack ADT A stack is a special case of a list Addition and removals can only be made at one end Last-in, first-out (LIFO) Push - adds a new item onto the stack Pop – removes and returns last item added Can be implemented with array or linked list Object Oriented Software Development 6. Arrays and collections 38

Stack applications Page visited history in a Web browser Undo sequence in a text editor Reversing a string Method calls – memory stack Balancing symbols – e.g. check for matching brackets in a C# code file Object Oriented Software Development 6. Arrays and collections 39

Queue ADT A queue is also special case of a list Addition can only be made at the tail and removals made at the head First-in, first-out (FIFO) Enqueue - adds a new item onto the queue Dequeue – removes and returns first item added Can be implemented with array or linked list Object Oriented Software Development 6. Arrays and collections 40

Queue applications Waiting lists Message queues Access to shared resources, e.g. printer Buffers for transferring data asynchronously e.g., file IO, sockets Object Oriented Software Development 6. Arrays and collections 41

Dictionary ADT A dictionary is a searchable collection of key-item pairs Also known as map, associative list Keys are used to locate items Collection can be sorted or unsorted May or may not allow more than one item with the same key – depends on implementation Object Oriented Software Development 6. Arrays and collections 42

Dictionary operations Put(key, item) - add a new key/item pair Get(key) – get the item with the specified key Set(key, item) – set the item for the specified key Remove(key) – remove the item with the specified key ContainsKey(key) – check whether the specified key exists Object Oriented Software Development 6. Arrays and collections 43

Dictionary data types Most reusable case is when key and item data types are both Object Can store any type and use any type as key because of polymorphism Sometimes it is convenient to have a customised implementation restricted to specific data types e.g..NET Framework includes a StringDictionary class Object Oriented Software Development 6. Arrays and collections 44

Boxing/unboxing with collections Collections often have item type Object Don’t need to create a separate collection class for each data type which might be stored A consequence of this is that value types are converted to object references (boxed) when stored in such a collection Object Oriented Software Development 6. Arrays and collections 45 boxing unboxing – cast to value type

Disadvantages of boxing Overhead of creating object references affects performance Need to write code to explicitly cast to value type Not type-safe – can add the “wrong” type of objects into the collection, compiler will not pick this up Object Oriented Software Development 6. Arrays and collections 46

Generics Generic collection classes give the best of both worlds One class allows storage of any data type Can specify what type it is to hold Type-safe – compiler will not allow code which stores wrong type Object Oriented Software Development 6. Arrays and collections 47 no boxing needed no unboxing, no cast needed same collection class used for different types

Generic list example CollectionsDemo solution SimpleCollections project SimpleGenericArrayList.cs Object Oriented Software Development 6. Arrays and collections 48

Creating a generic collection class Declare placeholder for generic type – T Placeholder will be replaced with specific type when application is compiled References to T instead of Object Object Oriented Software Development 6. Arrays and collections 49 generic type specific type becomes Employee[] entries when compiled

Generic collection class members Object Oriented Software Development 6. Arrays and collections 50 references to T instead of Object

Generic collection class members Can’t generally set a reference of generic type to null Type at compile time might be a value type which can’t be null Use default instead – null for reference type, default value for value type Object Oriented Software Development 6. Arrays and collections 51

Framework collections The.NET framework provides a range of collections in three namespaces System.Collections System.Collections.Generic generic versions – names not always consistent with non-generic variants, e.g. ArrayList is equivalent to List System.Collections.Specialized a range of collections with specialized characteristics Object Oriented Software Development 6. Arrays and collections 52

Framework collections Use framework collection where possible instead of writing your own Consider characteristics and likely impact on performance when choosing Use generic collections where possible Only create custom collections to match very specific, unusual requirements Object Oriented Software Development 6. Arrays and collections 53

Framework collections example FrameworkCollectionsDemo solution ListGenericDemo project Program.cs DictionaryGenericDemo project Program.cs Object Oriented Software Development 6. Arrays and collections 54

Using List Create Add Get value Iterate Object Oriented Software Development 6. Arrays and collections 55

Using Dictionary Create Add Get value Object Oriented Software Development 6. Arrays and collections 56 Note that.NET implementation uses Add rather than Put, and array-like index notation for lookup rather than Get

Using Dictionary Iterate Iterate through Dictionary as KeyValuePair collection Get dictionary values from Values property(can get Keys similarly) Object Oriented Software Development 6. Arrays and collections 57 could also use implicit type: var entry in dict

Further reading There are other collection types Many sources of information online Recommended: An Extensive Examination of Data Structures using C# 2.0 (MSDN) us/library/ms364091%28v=VS.80%29.aspx us/library/ms364091%28v=VS.80%29.aspx Object Oriented Software Development 6. Arrays and collections 58

Further reading The code in the CollectionsDemo download contains example implementations of the following collection types: Stack Queue Dictionary – see extra slides for information on ways to implement a Dictionary Implementation details of these will not be in the exam Object Oriented Software Development 6. Arrays and collections 59

What’s next? We will go on to look in detail at how to implement a UML model using C# classes, and how collections can help with this Object Oriented Software Development 6. Arrays and collections 60