Difference between revisions of "C Sharp List and ArrayList Collections"
m (Text replacement - "<google>BUY_CSHARP_ESSENTIALS_BOTTOM</google>" to "<htmlet>csharp</htmlet>") |
m (Text replacement - "<table border="0" cellspacing="0" width="100%">" to "<table border="0" cellspacing="0">") |
||
Line 1: | Line 1: | ||
− | <table border="0" cellspacing="0 | + | <table border="0" cellspacing="0"> |
<tr> | <tr> | ||
<td width="20%">[[Introducing C Sharp Arrays|Previous]]<td align="center">[[C Sharp Essentials|Table of Contents]]<td width="20%" align="right">[[Working with Strings in C Sharp|Next]]</td> | <td width="20%">[[Introducing C Sharp Arrays|Previous]]<td align="center">[[C Sharp Essentials|Table of Contents]]<td width="20%" align="right">[[Working with Strings in C Sharp|Next]]</td> | ||
Line 163: | Line 163: | ||
<hr> | <hr> | ||
− | <table border="0" cellspacing="0 | + | <table border="0" cellspacing="0"> |
<tr> | <tr> | ||
<td width="20%">[[Introducing C Sharp Arrays|Previous]]<td align="center">[[C Sharp Essentials|Table of Contents]]<td width="20%" align="right">[[Working with Strings in C Sharp|Next]]</td> | <td width="20%">[[Introducing C Sharp Arrays|Previous]]<td align="center">[[C Sharp Essentials|Table of Contents]]<td width="20%" align="right">[[Working with Strings in C Sharp|Next]]</td> |
Revision as of 14:32, 5 May 2016
Previous | Table of Contents | Next |
Introducing C# Arrays | Working with Strings in C# |
Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99 |
In the previous chapter we looked at C# Arrays. Whilst useful for many tasks arrays are really starting to show their age in terms of both functionality and flexibility. More advanced mechanisms for gathering groups of objects are provided by the C# Collection Classes.
What are C# Collection Classes
Creating C# List Collections - List<T> and ArrayList
Both the List<T> and ArrayList classes have properties very similar to C# arrays (for details on arrays refer to Introducing C# Arrays. One key advantage of these classes over arrays is that they can grow and shrink as the number of stored objects changes.
The List<T> class is contained with the System.Collections.Generic namespace whilst the ArrayList class is contained within the System.Collections namespace.
The syntax for creating a List<T> collection is as follows:
List<type> name = new List<type>();
An ArrayList object is created in a similar manner, although without the type argument:
ArrayList name = new ArrayList();
With the above syntax in mind we can now create a List<T> object called colorList:
using System; using System.Collections.Generic; public class Lists { static void Main() { List<string> colorList = new List<string>(); } }
Adding Items to Lists
Once a List object has been created there are a number of methods which may be called to perform tasks on the list. Once such method is the Add() method which, as the name suggests, is used to add items to the list object:
List<string> colorList = new List<string>(); colorList.Add ("Red"); colorList.Add ("Green"); colorList.Add ("Yellow"); colorList.Add ("Purple"); colorList.Add ("Orange");
Accessing List Items
Individual items in a list may be accessed using the index value of the item (keeping in mind that the first item is index 0, the second index 1 and so on). The index value is placed in square brackets after the list name. For example, to access the second item in the colorList object:
Console.WriteLine (colorList[1]);
A list item value can similarly be changed using the index combined with the assignment operator. For example, to change the color from Yellow to Ingido:
colorList[2] = "Indigo";
All the items in a list may be accessed using a foreach loop. For example:
foreach (string color in colorList) { Console.WriteLine ( color ); }
When compiled and executed, the above code will output each of the color strings in the colorList object.
Removing Items From Lists
Items may be removed from a list using the Remove() method. This method takes the value of the item to be removed as an argument. For example, to remove the "Red" string from the colorList object:
colorList.Remove("Red");
It is important to note that items in a list may be duplicated. In the case of duplicated items, the Remove() method will only remove the first matching instance.
Inserting Items into a C# List
Previously we used the Add() method to add items to a list. The Add() method, however, only adds items to the end of a list. Sometimes it is necessary to insert a new item at a specific location in a list. The Insert() method is provided for this specific purpose. Insert() takes two arguments, an integer indicating the index location of the insertion and the item to be inserted at that location. For example, to insert an item at location 2 in our example list:
colorList.Insert(2, "White");
Sorting Lists in C#
There is no way to tell C# to automatically sort a list as items are added. If the items in a list are required to be always sorted into order the Sort() method should be called after new items are added:
colorList.Sort();
Finding Items in a C# List or ArrayList
A number of methods are provided with the List and ArrayList classes for the purposes of finding items. The most basic method is the Contains() method, which when called on a List or ArrayList object returns true if the specified item is found in the list, or false if it is not.
The IndexOf() method returns the index value of a matching item in a list. For example, the following code sample will output the value 2, which is the index position of the "Yellow" string:
List<string> colorList = new List<string>(); colorList.Add ("Red"); colorList.Add ("Green"); colorList.Add ("Yellow"); colorList.Add ("Purple"); colorList.Add ("Orange"); Console.WriteLine(colorList.IndexOf("Yellow"));
If the item is not found in the List a value of -1 is returned by the IndexOf() method.
This technique could be used to replace a specific value with another. For example, without knowing in advance the index value of the "Yellow" string we can change to "Black":
colorList[colorList.IndexOf("Yellow")] = "Black";
The LastIndexOf() method returns the index value of the last item in the list to match the specified item. This is particularly useful when a list contains duplicate items.
Obtaining Information About a List
There are two class members that are useful for obtaining information about a C# List or ArrayList collection object. The Capacity property can be used to identify how many items a collection can store without having to resize.
The Count property, on the other hand, identifies how many items are currently stored in the list. For obvious reasons, Capacity will always exceed Count.
In instances where a large gap exists between Count and Capacity the excess capacity may be removed with a call the TrimExcess() method.
Clearing and Trimming C# Lists
All the items in a list may be removed using the Clear() method:
colorList.Clear();
The Clear() method removes the items from the list and sets the Count property to zero. The Capacity property, however, remains unchanged. To remove the capacity of a list follow the Clear() method call with a call to TrimExcess().
Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99 |
Previous | Table of Contents | Next |
Introducing C# Arrays | Working with Strings in C# |