Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10 List Richard Gesick.

Similar presentations


Presentation on theme: "Lecture 10 List Richard Gesick."— Presentation transcript:

1 Lecture 10 List Richard Gesick

2 Objectives Review static vs. dynamic collections
Discuss dynamic collection API Utilize a collection (example)

3 Arrays Allocate a contiguous section of memory Homogeneous
Access via index <type> NAME[]; NAME = new <type>[SIZE];

4 “Dynamic” Arrays Arrays are good if you know how large the collection will be But if this isn’t known beforehand, how big is enough? One approach: Allocate for N When N is exceeded Allocate 2N and copy N into new space Free up original space for N Time consuming (memory allocation/copy)

5 Reallocating an Array Original When full A A

6 Reallocating an Array Original When full A A B

7 Reallocating an Array Original When full A A B

8 Reallocating an Array Original When full A A B

9 Reallocating an Array Original When full A A B null

10 Reallocating an Array Original When full A Garbage Collection A B null

11 Collection APIs Let’s not reinvent the wheel
Other APIs provide access to collections using System.Collection.Generic; List – single-reference (one-way) collection LinkedList – double-reference (two-way) Queue – first in, first out (FIFO) Stack – first in, last out (FILO) Dictionary – key/value pairs Methods to sort, search, iterate across these collections

12 Generic or Parameterized Collections
To overcome the limitations of polymorphic collections, modern languages also provide a means by which you can explicitly state the type of thing you want the collection to manage. You do this via a "parameter" to the class when you define it. List<T> is called a generic class because it can be used with any type of object. T is a placeholder for the type of the objects stored in the list.

13 Focus on List<T>
List provides for growth (dynamic) Has capacity & count Capacity = size allocated Count = number of slots used Reallocated automatically Grows when full to 2x Provides pseudo-immediate access Gory details are hidden from us

14 List API List<T>() - Constructor (to hold stuff of type T)
Add(T) - Adds object of type T to end of list Remove(T) - Deletes first occurrence of match RemoveAt(int) - Removes the object at position Sort(…) – arranges the objects Clear() – removes all objects Insert(T, int) - Adds object at specified position Searching: Contains/Exists/Find/FindAll/FindIndex/FindLast

15 List The Add method appends its argument to the end of the List.
The Insert method inserts a new element at the specified position. The first argument is an index—as with arrays, collection indices start at zero. The second argument is the value that is to be inserted at the specified index. All elements at the specified index and above are shifted up by one position.

16 List The Count property returns the number of elements currently in the List. Lists can be indexed like arrays by placing the index in square brackets after the List variable’s name. The Remove method is used to remove the first instance of an element with a specific value. If no such element is in the List, Remove does nothing. RemoveAt removes the element at the specified index; all elements above that index are shifted down by one.

17 Using List List<int> numbers = new List<int>(); numbers.Add(42); numbers.Add(9); Since the type that the collection holds is known, we can do something like this: foreach(int i in numbers) {   Console.WriteLine(i); } And you can also access the elements via the [] index notation as in: numbers[0] = 8;

18 Summary Array List More details of C# List<T>
Fixed in size (too small/large?) Immediate access via index Don’t forget to allocate each element if needed List Dynamic in size Pseudo-immediate access More details of C# List<T>


Download ppt "Lecture 10 List Richard Gesick."

Similar presentations


Ads by Google