19.1. ArrayList

An ArrayList encapsulates an ordered list of objects, much like Java's Vector or ArrayList classes. Each ArrayList object has an initial capacity (accessible via the public property Capacity) which automatically expands as more elements are inserted into the ArrayList object.

The example below shows some common methods which you can use with ArrayList.

 1: using System;
 2: using System.Collections;
 3:
 4: public class TestClass{
 5:   public static void Main(){
 6:
 7:     String item1 = "apple",
 8:            item2 = "orange",
 9:            item3 = "banana",
10:            item4 = "durian",
11:            item5 = "strawberry";
12:
13:     ArrayList al = new ArrayList();
14:
15:     // Use of Add. New elements are inserted behind.
16:     al.Add(item1);
17:     al.Add(item2);
18:     al.Add(item3);
19:     al.Add(item4);
20:     al.Add(item5);
21:
22:     // Use of Contains and IndexOf
23:     Console.WriteLine
24:       ("al contains orange : " + al.Contains("orange"));
25:     Console.WriteLine
26:       ("Index of orange : " + al.IndexOf("orange"));
27:     PrintCollection(al);
28:     Console.WriteLine("------------------------------");
29:
30:     // Use of Insert. Unlike Add, you can specify a
31:     // position to insert an element.
32:     Console.WriteLine("Inserting peach at index 1...");
33:     al.Insert(1,"peach");
34:     Console.WriteLine
35:       ("Index of peach  : " + al.IndexOf("peach"));
36:     Console.WriteLine
37:       ("Index of orange : " + al.IndexOf("orange"));
38:     PrintCollection(al);
39:     Console.WriteLine("------------------------------");
40:
41:     // Use of Count public property
42:     Console.WriteLine("Count: " +al.Count);
43:
44:     // Use of Remove
45:     PrintCollection(al);
46:     Console.WriteLine("Removing banana...");
47:     al.Remove("banana");
48:     PrintCollection(al);
49:     Console.WriteLine("------------------------------");
50:
51:     // Use of Sort
52:     PrintCollection(al);
53:     Console.WriteLine("Sorting...");
54:     al.Sort();
55:     PrintCollection(al);
56:     Console.WriteLine("------------------------------");
57:
58:     // Use of ToArray. ToArray returns an object array
59:     Console.WriteLine("Creating new array...");
60:     object[] myArray = al.ToArray();
61:     Console.WriteLine("Array size: " + myArray.Length);
62:   }
63:
64:   // prints out all elements of the ArrayList object
65:   public static void PrintCollection(ArrayList al){
66:       Console.Write("Elements: ");
67:
68:       IEnumerator enumerator = al.GetEnumerator();
69:
70:       while (enumerator.MoveNext())
71:          Console.Write(enumerator.Current + ",");
72:
73:       Console.WriteLine(""); // new line
74:   }
75: }

Output:

c:expt>test
al contains orange : True
Index of orange : 1
Elements: apple,orange,banana,durian,strawberry,
------------------------------
Inserting peach at index 1...
Index of peach  : 1
Index of orange : 2
Elements: apple,peach,orange,banana,durian,strawberry,
------------------------------
Count: 6
Elements: apple,peach,orange,banana,durian,strawberry,
Removing banana...
Elements: apple,peach,orange,durian,strawberry,
------------------------------
Elements: apple,peach,orange,durian,strawberry,
Sorting...
Elements: apple,durian,orange,peach,strawberry,
------------------------------
Creating new array...
Array size: 5

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.233.43