Arrays vs Collections

arrays and collections in c# and arrays and collections in c# with examples
Dr.MasonHanks Profile Pic
Dr.MasonHanks,Germany,Teacher
Published Date:23-07-2017
Your Website URL(Optional)
Comment
Object Oriented Software Development 6. Arrays and collections www.ThesisScientist.comOne-to-many relationships  A department can have many employees in it  One-to-many “has-a” relationship Employee -employeeId Department -employees -name -username -phoneNumber 1 +Move() +Email() HourlyPaidEmployee SalariedEmployee -payGrade +RecordTime() +PayIncrement() +Email()  “has-a” relationship is implemented with instance variable  This variable must be able to hold a collection of Employee objects www.ThesisScientist.comCollections 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 www.ThesisScientist.comArrays  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 www.ThesisScientist.comLooping 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 www.ThesisScientist.comArrays 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 www.ThesisScientist.comMore 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 www.ThesisScientist.comLimitations 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 www.ThesisScientist.comAbstract 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 www.ThesisScientist.comList  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 ,a ,a .....a . 1 2 3 n The size of this list is n.  The first element of the list is a ,and the last 1 element is a n  The position of element a in a list is i. i www.ThesisScientist.comList 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 www.ThesisScientist.comLists 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 www.ThesisScientist.comImplementing a list  We can express these requirements as an interface  Lists will implement this interface www.ThesisScientist.comData 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 www.ThesisScientist.comImplementation 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 www.ThesisScientist.comDoes 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 www.ThesisScientist.comSimple 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 www.ThesisScientist.comSimpleArrayList  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 constructor instance variables www.ThesisScientist.comAdding 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 www.ThesisScientist.comAdding items to SimpleArrayList  Adding a new item at a specified index involves more work  Need to shuffle items up to make space G l s g o w G l s g o w G l s g o w a www.ThesisScientist.com