Download presentation
Presentation is loading. Please wait.
1
CS313D: Advanced Programming Language
Computer Science department Lecture 8 : Collections
2
Lecture Contents Why collections? What is a collection?
Non-generic collections: Array & ArrayList Stack HashTable dr. Amal Khalifa, Spr 17
3
Why collections? Data structures are essential to build apps, but there’s no need to build such them from scratch. Instead, you can use the prepackaged collection classes provided by the .NET Framework. System.Collections contains collections that store references to objects. dr. Amal Khalifa, Spr 17
4
What is a collection? Collection classes store collections of data
Each instance of one of these classes is a collection of items. Examples: Arrays Stacks Queues HashTables dr. Amal Khalifa, Spr 17
5
interfaces of the .NET Framework collections
Implementations of these interfaces are provided within the framework. dr. Amal Khalifa, Spr 17
6
interfaces of the .NET Framework collections
GetEnumerator. Some collection types offer the GetEnumerator method. This method returns an Enumerator object that can be used to loop through the collection. Complete knowledge of the actual collection type is not needed. interfaces of the .NET Framework collections Implementations of these interfaces are provided within the framework. dr. Amal Khalifa, Spr 17
7
collection classes dr. Amal Khalifa, Spr 17
8
Class Array and Enumerators
Use Array static methods to: Copy arrays Sort array elements Search array elements dr. Amal Khalifa, Spr 17
9
Line 22 uses static Array method Sort to sort array doubleValues.
When this method returns, the array contains its original elements sorted in ascending order. Line 25 uses static Array method Copy to copy elements from array intValues to array intValuesCopy. dr. Amal Khalifa, Spr 17
10
Line 22 uses static Array method Sort to sort array doubleValues.
When this method returns, the array contains its original elements sorted in ascending order. Line 22 uses static Array method Sort to sort array doubleValues. When this method returns, the array contains its original elements sorted in ascending order. Line 25 uses static Array method Copy to copy elements from array intValues to array intValuesCopy. dr. Amal Khalifa, Spr 17
11
Lines 32 and 40 invoke static Array method BinarySearch to perform binary searches on array intValues. Method BinarySearch receives the sorted array in which to search and the key for which to search. The method returns the index in the array at which it finds the key (or a negative number if the key was not found). BinarySearch assumes that it receives a sorted array. Its behavior on an unsorted array is unpredictable dr. Amal Khalifa, Spr 17
12
Line 25 uses static Array method Copy to copy elements from array intValues to array intValuesCopy.
Lines 32 and 40 invoke static Array method BinarySearch to perform binary searches on array intValues. Method BinarySearch receives the sorted array in which to search and the key for which to search. The method returns the index in the array at which it finds the key (or a negative number if the key was not found). BinarySearch assumes that it receives a sorted array. Its behavior on an unsorted array is unpredictable dr. Amal Khalifa, Spr 17
13
BinarySearch assumes that it receives a sorted array.
Lines 32 and 40 invoke static Array method BinarySearch to perform binary searches on array intValues. Method BinarySearch receives the sorted array in which to search and the key for which to search. The method returns the index in the array at which it finds the key (or a negative number if the key was not found). BinarySearch assumes that it receives a sorted array. Lines 32 and 40 invoke static Array method BinarySearch to perform binary searches on array intValues. Method BinarySearch receives the sorted array in which to search and the key for which to search. The method returns the index in the array at which it finds the key (or a negative number if the key was not found). BinarySearch assumes that it receives a sorted array. Its behavior on an unsorted array is unpredictable dr. Amal Khalifa, Spr 17
14
Enumerator!! GetEnumerator returns an enumerator that can iterate over the collection. dr. Amal Khalifa, Spr 17
15
Class Array and Enumerators
Other static Array methods include Clear (to set a range of elements to 0, false or null, as appropriate) CreateInstance (to create a new array of a specified type) IndexOf (to locate the first occurrence of an object in an array or portion of an array) LastIndexOf (to locate the last occurrence of an object in an array or portion of an array) Reverse (to reverse the contents of an array or portion of an array). dr. Amal Khalifa, Spr 17
16
ArrayList dr. Amal Khalifa, Spr 17
17
Class ArrayList ArrayList collection class mimics the functionality of conventional arrays and provides dynamic resizing of the collection through the class’s methods. At any time, an ArrayList contains a certain number of elements less than or equal to its capacity—the number of elements currently reserved for the ArrayList. An app can manipulate the capacity with ArrayList property Capacity. dr. Amal Khalifa, Spr 17
18
dr. Amal Khalifa, Spr 17
19
Example Use an ArrayList to store string objects
Use its methods to add, remove, search elements dr. Amal Khalifa, Spr 17
20
Code ArrayList constructor accepts a pre- allocated array!!
dr. Amal Khalifa, Spr 17
21
Code Count vs. Capacity IndexOf returns the position of the element or a negative value if not found We use the indexer to obtain each of secondList’s elements, then remove each one from firstList with the Remove method. This method deletes a specified item from an ArrayList by performing a linear search and removing (only) the first occurrence of the specified object. All subsequent elements shift toward the beginning of the ArrayList to fill the emptied position. dr. Amal Khalifa, Spr 17
22
code Using Indexers Remove method applies a linear search and deletes only the first occurrence dr. Amal Khalifa, Spr 17
23
ArrayList & polymorphism
1 2 3 positions elements S l f dr. Amal Khalifa, Spr 17
24
Class Stack dr. Amal Khalifa, Spr 17
25
What is a stack? LIFO data structures dr. Amal Khalifa, Spr 17
26
Example Using directive Creating a stack of Object references
Elements of different types boxing dr. Amal Khalifa, Spr 17
27
Example Push adds an object on the top of the stack grows to accommodate new elements Pop removes the object on the top of the stack peek doesn’t remove the top element dr. Amal Khalifa, Spr 17
28
Example Printing elements without modification
property Count obtains the number of elements in stack. Although Fig does not demonstrate it, class Stack also has method Contains, which returns true if the Stack contains the specified object, and returns false otherwise. dr. Amal Khalifa, Spr 17
29
Output dr. Amal Khalifa, Spr 17
30
Class Hashtable dr. Amal Khalifa, Spr 17
31
What is a Hashtable? When we convert a key into an array index, we literally scramble the bits, making a “hash” of the number. dr. Amal Khalifa, Spr 17
32
Why Hashtable ? Storing Large data, efficient retrieve, saving memory space Example: 100 employees with nine-digit social security numbers you want to store and retrieve employee data by using the social security number as a key nominally require an array with 1,000,000,000 elements. Hashing: a high-speed scheme for converting keys such as social security numbers and inventory part numbers to unique array indices. dr. Amal Khalifa, Spr 17
33
Hashing when an app needs to store something, the scheme could convert the key rapidly to an index and the record of information could be stored at that location in the array Retrieval occurs the same way— but in reverse. dr. Amal Khalifa, Spr 17
34
Collisions two different keys “hash into” the same cell, or element, in the array we cannot sort two different data records to the same space Solutions: “hash again” - find an alternative home for all records beyond the first that hash to a particular array index. hash “bucket” - a linked list of all the key–value pairs that hash to that cell dr. Amal Khalifa, Spr 17
35
Hash Function A hash function performs a calculation that determines where to place data in the hash table. The hash function is applied to the key in a key– value pair of objects. Class Hashtable can accept any object as a key. class object defines method GetHashCode, which all objects inherit. dr. Amal Khalifa, Spr 17
36
Example Creating a HashTable of <string, int>
dr. Amal Khalifa, Spr 17
37
Example unbox and box the int data stored in the Hashtable every time it incremented the count for a particular key inefficient Downcasting boxing & untboxing If a word appears twice, line 38 will try to downcast this string to an int, causing an InvalidCastException at execution time. The error that appears at execution time will indicate that the problem is at line 38, where the exception occurred, not at line 42. This makes the error more difficult to find and debug, especially in large apps where the exception may occur in a different file—and even in a different assembly. dr. Amal Khalifa, Spr 17
38
Example If we need to use any of its string- specific methods, we need an explicit downcast. The enumerator of a Hashtable uses the DictionaryEntry structure to store key–value pairs. dr. Amal Khalifa, Spr 17
39
dr. Amal Khalifa, Spr 17
40
Take care!! dr. Amal Khalifa, Spr 17
41
That’s all,,,, Chapter 21: 21.1 21.4 dr. Amal Khalifa, Spr 17
42
Case Study dr. Amal Khalifa, Spr 17
43
dr. Amal Khalifa, Spr 17
44
dr. Amal Khalifa, Spr 17
45
dr. Amal Khalifa, Spr 17
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.