Книга: C# 2008 Programmer

Dynamic Arrays Using the ArrayList Class

Dynamic Arrays Using the ArrayList Class

Arrays in C# have a fixed size once they are initialized. For example, the following defines a fixed- size array of five integer elements:

int[] num = new int[5];

If you need to dynamically increase the size of an array during runtime, use the ArrayList class instead. You use it like an array, but its size can be increased dynamically as required.

The ArrayList class is located within the System.Collections namespace, so you need to import that System.Collections namespace before you use it. The ArrayList class implements the IList interface.

To use the ArrayList class, you first create an instance of it:

ArrayList arrayList = new ArrayList();

Use the Add() method to add elements to an ArrayList object:

arrayList.Add("Hello");
arrayList.Add(25);
arrayList.Add(new Point(3,4));
arrayList.Add(3.14F);

Notice that you can add elements of different types to an ArrayList object.

To access an element contained within an ArrayList object, specify the element's index like this:

Console.WriteLine(arrayList[0]); //---Hello---
Console.WriteLine(arrayList[1]); //---25---
Console.WriteLine(arrayList[2]); //---{X=3,Y=4}
Console.WriteLine(arrayList[3]); //---3.14--- 

The ArrayList object can contain elements of different types, so when retrieving items from an ArrayList object make sure that the elements are assigned to variables of the correct type. Elements retrieved from an ArrayList object belong to Object type.

You can insert elements to an ArrayList object using the Insert() method:

arrayList.Insert(1, " World!");

After the insertion, the ArrayList object now has five elements:

Console.WriteLine(arrayList[0]); //---Hello---
Console.WriteLine(arrayList[1]); //---World!---
Console.WriteLine(arrayList[2]); //---25---
Console.WriteLine(arrayList[3]); //---{X=3,Y=4}---
Console.WriteLine(arrayList[4]); //---3.14---

To remove elements from an ArrayList object, use the Remove() or RemoveAt() methods:

arrayList.Remove("Hello");
arrayList.Remove("Hi"); //---cannot find item---
arrayList.Remove(new Point(3, 4));
arrayList.RemoveAt(1);

After these statements run, the ArrayList object has only two elements:

Console.WriteLine(arrayList[0]); //---World!---
Console.WriteLine(arrayList[1]); //---3.14--- 

If you try to remove an element that is nonexistent, no exception is raised (which is not very useful). It would be good to use the Contains() method to check whether the element exists before attempting to remove it:

if (arrayList.Contains("Hi")) arrayList.Remove("Hi");
else Console.WriteLine("Element not found.");

You can also assign the elements in an ArrayList object to an array using the ToArray() method:

object[] objArray;
objArray = arrayList.ToArray();
foreach (object o in objArray)
 Console.WriteLine(o.ToString());

Because the elements in the ArrayList can be of different types you must be careful handling them or you run the risk of runtime exceptions. To work with data of the same type, it is more efficient to use the generic equivalent of the ArrayList class — the List<T> class, which is type safe. To use the List<T> class, you simply instantiate it with the type you want to use and then use the different methods available just like in the ArrayList class:

List<int> nums = new List<int>();
nums.Add(4);
nums.Add(1);
nums.Add(3);
nums.Add(5);
nums.Add(7);
nums.Add(2);
nums.Add(8);
//---sorts the list---
nums.Sort();
//---prints out all the elements in the list---
foreach (int n in nums) Console.WriteLine(n);

If you try to sort an ArrayList object containing elements of different types, you are likely to run into an exception because the compiler may not be able to compare the values of two different types.

Оглавление книги


Генерация: 1.059. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз