EE 356 Fall 2016 C# WPF – Lists and Queues

advertisement
May 13, 2016
EE 356
C# WPF – Lists and Queues
Fall 2016
Lists
List<T> This is a generic type which can hold any standard type (int, double, etc) or class. The List
automatically resizes itself to fit the amount of data that it holds so it is not necessary to set a dimension.
For example the following creates a list of integers:
List<int> intList = new List<int>();
The common methods and properties that can be done on the list include:
intList.Add(i);
intLst[0]);
intList.Count;
intList.BinarySearch(5);
intList.Clear();
intList.Contains(12);
intList.IndexOf(6);
intList.Insert(5, 98);
intList.Max();
intList.Min();
intList.Remove(5);
intList.Reverse();
intList.Sort();
intList.ToArray();
//Add i to the list as the last item
//Index any item in the list for access
//Total number of items in the list
//assumes the list is in order and returns the index
//Clear the list
//Returns true if 12 is in the list
//Finds the index of 6
//puts 5 at index 98
//Max and min values in list
//Removes 5 from the list
//Reverse the list
//Sort the list
//Change the list to an array
You can also have a list of lists:
List<List<string>> lenList =new List<List<string>>();
//Create two new string lists: s1List and s2List
List<string> s1List = new List<string>();
List<string> s2List = new List<string>();
//Add these two string lists to lenList
lenList.Add(s1List);
lenList.Add(s2List);
This may be useful if you read in a word list which is sorted by length.
Queues
A queue is a first-in-first-out buffer. A queue typically has a pointer to the location of the next item
going in and a pointer to the next item coming out. If the two pointers are equal the queue is empty. In
C# you can create a queue like this:
Queue<int> intQ = new Queue<int>();
The common methods and properties that can be done on the queus include:
intQ.Clear(); //Clears the queue
intQ.Count(); //Returns the number of items in the queue
intQ.Dequeue(); //Returns the next item from the queue
intQ.Enqueue(5);//Put the argument into the queue
intQ.Peek();
//Looks at the item on the queue
You can put any standard type into a queue as well as classes.
Download