Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 21 1. 2  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.

Similar presentations


Presentation on theme: "Chapter 21 1. 2  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access."— Presentation transcript:

1 Chapter 21 1

2 2  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access needs.  Store objects

3 The.NET Framework has 3 collection namespaces:  System.Collections  contains collections that store references to objects  there’s a large amount of legacy code in industry that uses these collections  Not included in the default C# template  System.Collections.Generic - new applications should use it the namespace, which contains generic classes:  Included in the default C# template  List  Dictionary...  Since C# 2.0  Similar to C++ STL (templates)  The System.Collections.Specialized namespace contains several collections that support specific types, such as strings and bits.  msdn.microsoft.com/en-us/library/system.collections.specialized.aspx.msdn.microsoft.com/en-us/library/system.collections.specialized.aspx  The collections in these namespaces provide standardized, reusable components - no need to write your own collection classes (kind of).  They’re tuned for rapid execution and for efficient use of memory.  As new data structures and algorithms are developed that fit this framework, a large base of programmers already will be familiar with the interfaces and algorithms implemented by those data structures. 3

4  System.Collections and S ystem.Collections.Specialized - earlier versions of C#/.NET namespaces  Manipulated object references, can store any object in a collection.  Disadvantage: when retrieving them from a collection -> needs to process specific types of objects.  Need to be downcast to an appropriate type to allow the application to process the objects correctly.  System.Collections.Generic - uses the generics capabilities  Many classes are simply generic counterparts of the classes in System.Collections. you can specify the exact type that will be stored in a collection.  Benefits: of compile-time type checking—the compiler ensures that you’re using appropriate types with your collection and, if not, issues compile-time error messages.  Once you specify the type stored in a collection, any item you retrieve from the collection will have the correct type.  Eliminates the need for explicit type casts that can throw InvalidCastExceptions at execution time if the referenced object is not of the appropriate type.  Eliminates the overhead of explicit casting, improving efficiency and type safety. Generic collections are especially useful for storing structs, since they eliminate the overhead of boxing and unboxing. 4

5 5  List is the generic List class  T represents a class name parameter to be supplied in declarations.  Provides traditional list operations  Insert  Delete  Also provides array operations  Access by position, using index notation

6 6  Clear()  Removes all items from the list  bool Contains(T item)  Determines if item is in the list  int IndexOf(T item)  Returns index of item, or -1 if not in list.  Sort()... more

7 7  Array index notation can be used to get or set a specified item.  int_list[5] = 17;  int temp = int_list[5]; int_list[5]  Throws an exception if int_list[5] does not exist. List Properties Count Count Number of items in the list

8 8 static void Main(string[] args) { List ints = new List (); ints.Add(1); ints.Add(2); ints.Add(3); foreach (int i in ints) { Console.WriteLine(i.ToString() ); } Console.ReadLine(); }

9 System.Collections and have analog in System.Collections.Generic 9

10 21.2 Collections Overview cont 10

11 21.2 Collections Overview cont cont 11

12 12 Ascending order

13 13 sortedsorted keykey

14 14

15 15

16 16

17 Line 54: an enumerator is returned by the GetEnumerator initially positioned before the first element in Array doubleValues(GetEnumerator (54 and 62) always returns an enumerator positioned before the first element. Line 56: MoveNext in the first iteration of the while loop, the enumerator advances to the first element in doubleValues. Lines 56–57: loops over each element until the enumerator passes the end of doubleValues and MoveNext returns false. In each iteration, the enumerator’s Current property to obtain and output the current array element. ( PrintArrays is called twice (lines 19 and 28), so GetEnumerator is called twice on doubleValues) IEnumerator property Current is read-only. Enumerators cannot be used to modify the contents of collections, only to obtain the contents. Enumerations and foreach 17

18 Lines 70–71: foreach iterates over the collection elements like an enumerator. In fact, the foreach behaves exactly like an enumerator -> both loop over the elements of an array one by one in consecutive order. Neither allows you to modify the elements during the iteration. This is not a coincidence! The foreach statement implicitly obtains an enumerator via the GetEnumerator method and uses the enumerator’s MoveNext method and Current property to traverse the collection, just as explicitly in lines 54–57. foreach statement to iterate over any collection that implements the IEnumerable interface—not just arrays ( ArrayList ) Enumerations and foreach cont 18

19 static 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 Array Class 19

20 21.4.1 Class ArrayList (Nongeneric) 20

21 21.4.1 Class ArrayList (Nongeneric) 21 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 New apps should use the generic List class (the generic version of ArrayList )

22 22

23 23

24 24

25 25

26 26

27 27

28 28

29 The Stack class implements a stack data structure and provides much of the functionality that is defined in Deitel’s implementation in Section 21.5. 29

30 30

31 31

32 32

33 33 Method Push takes an object as an argument and inserts it at the top of the Stack. Method PrintStack (lines 50–64) uses Stack property Count (implemented to fulfill the contract of interface ICollection ) to obtain the number of elements in stack. Method Peek returns the value of the top stack element but does not remove the element from the Stack. Method Pop takes no arguments—it removes and returns the object currently on the Stack’s top.

34  Sorting and retrieving information with arrays is efficient if some aspect of your data directly matches the key value and if those keys are unique and tightly packed.  Self-study HERE

35 35

36 36

37 37

38 38

39 39

40 40 SortedDictionary

41 41

42 42

43 43

44 44

45 45

46 Generic Class LinkedList 46

47 47

48 48

49 49

50 50

51 51


Download ppt "Chapter 21 1. 2  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access."

Similar presentations


Ads by Google