C#. The ArrayList class




The ArrayList class


Contents


Search other websites:


1. To what type of collections is owned the ArrayList class?

Answer: to not-generalized collections.

To include the not-generalized collection, is needed to specify in the program:

using System.Collections;

 


2. What interfaces are realized in the ArrayList class?

In the ArrayList class are realized following interfaces:

ICollection, IList, IEnumerable, ICloneable.

 


3. How to create an empty collection ArrayList?

In the string below is created the empty collection of type ArrayList named al:

ArrayList al = new ArrayList();

 


4. How to create an ArrayList of the 25 items?
ArrayList al = new ArrayList(25);

 


5. How to add a new item in the collection ArrayList?

To add a new item in the collection ArrayList you need use method Add(). In the example below is formed a dynamic array that contains 3 items: [12, 23, 88].

al.Add(12); // add number 12 to dinamic array
al.Add(23); // add number 23 to dinamic array
al.Add(88); // add number 88 to dinamic array

Added items are the type object.

C# ArrayList items figure Figure 1. Type of ArrayList items

 


6. How to define the number of items of the array of ArrayList type? Property Count.

For this is used property Count.

Example.

ArrayList al = new ArrayList();
al.Add(10);
al.Add(20);
al.Add(30);

// number of elements
int n;
n = al.Count; // n = 3

 


7. How to convert array of ArrayList type into the integer array? Demonstration of method ToArray().

In the example below is converted the array al of type ArrayList into the array ar of integer numbers. Also, the sum of array ar items is calculated.

ArrayList al = new ArrayList();

// add three numbers to dinamic array
al.Add(10);
al.Add(20);
al.Add(30);

int sum = 0;

// array al converting to array ar of integer numbers
int[] ar = (int[])al.ToArray(typeof(int));

// calculating the sum
for (int i = 0; i < ar.Length; i++)
{
    sum += ar[i];
}

label1.Text = sum.ToString(); // output of sum value on the form

 


8. Organization of work of ArrayList class with the structures of type the structure BOOK.
8.1. Organization of structures array of “BOOK” type. Method Add().

Let there be given type of structure BOOK:

struct BOOK
{
    public string Author;
    public string Title;
    public int year;
    public double price;
};

Let’s organize a dynamic array of type “struct BOOK” using the ArrayList class.

Answer:

BOOK b1 = new BOOK();
ArrayList al = new ArrayList();

// create a first book
b1.Author = "Author-1";
b1.Title = "Title-1";
b1.year = 1998;
b1.price = 90.95;

al.Add(b1); // add book to array

// create second book
b1.Author = "Author-02";
b1.Title = "Title-02";
b1.year = 1980;
b1.price = 130.94;

al.Add(b1); // add book to array

 


8.2. How to convert array ArrayList to array of structures? Demonstration of method ToArray() for structure of type BOOK.

In the example below is realized the converting of dynamic array al of type ArrayList into the array of BOOK structures. The name of array is ar.

BOOK[] ar = (BOOK[])al.ToArray(typeof(BOOK));

 


8.3. How to define the number of items of type BOOK structure into the array ArrayList?

Let there be given a structure of type “BOOK” with entered data about a two books (see p.8.1). To get the number of entered books, need to use the property Count.

int n;
n = al.Count; // n = 2

 


9. How delete all items from array of type ArrayList? Property Clear().
ArrayList al = new ArrayList();
al.Add(10); // add numbers to array al
al.Add(20);
al.Add(30);

// delete all items from array
al.Clear(); // al.Count = 0

 


10. How to delete the item from array ArrayList? Method Remove().

Example 1. Deleting the item 20 from ArrayList.

ArrayList al = new ArrayList();
al.Add(10); // add three numbers
al.Add(20);
al.Add(30);
al.Remove(20); // item with value 20 deleted

Command Remove() removes the first occurrence of item with a specified value value. That is, if in the list of several items is a value of 20 then only be removed first element.

Example 2. Removing of several items 20 from array ArrayList.

ArrayList al = new ArrayList();
al.Add(10);
al.Add(20);
al.Add(30);
al.Add(20);
al.Add(20); // al = [10, 20, 30, 20, 20]

al.Remove(20); // al = [10, 30, 20, 20]
al.Remove(20); // al = [10, 30, 20]

 


11. How to copy the object of type ArrayList into another memory? Method Clone().

If you perform the assignment of two ArrayList objects then they will refer to the same memory location. An example:

ArrayList al = new ArrayList();
ArrayList al2;
al.Add(10);
al.Add(20);
al.Add(30);

al2 = al; // al2 = al = [10, 20, 30] - point to the same memory location
al2.Remove(20); // al = [10, 30]

C# variables memory figureFigure 2. Variables al and al2 point to the general area in memory

If is necessary to copy from a variable al with allocating additional memory then need to use method Clone(). In this case variables “al” and “al2” point to different memory areas (Figure 3).

ArrayList al = new ArrayList();
ArrayList al2;
al.Add(10);
al.Add(20);
al.Add(30);
al2 = (ArrayList)al.Clone();
al2.Remove(20); // al = [10, 20, 30]; al2 = [10, 30]

C# Clone() method collection figureFigure 3. By using a Clone() method is realized copying data from al to al2 with simultaneously memory allocation

 


12. Determining whether an item in an ArrayList array. Method Contains().

Method Contains() returns true, when item is in the ArrayList. Otherwise, it returns false.

ArrayList al = new ArrayList();
al.Add(25);
al.Add(-18);
al.Add(33);
bool is_el;
is_el = al.Contains(-18); // is_el = True
is_el = al.Contains(50); // is_el = False

 


13. How select the part of array ArrayList? Method GetRange().

Method GetRange() returns array of type ArrayList, that is a subset of current array ArrayList.

Example.

ArrayList al = new ArrayList();
ArrayList al2;

al.Add(0);
al.Add(1);
al.Add(2);
al.Add(3);
al.Add(4);
al.Add(5); // al = [0, 1, 2, 3, 4, 5]
al2 = al.GetRange(2, 3); // al2 = [2, 3, 4]

 


14. An example of sum calculation of items of ArrayList array.

In the example below is calculated the sum of items of ArrayList array. Items are converted to double type.

ArrayList al = new ArrayList();
double sum = 0;

al.Add(0.6);
al.Add(-10.45);
al.Add(2.667);
al.Add(300.78);

for (int i = 0; i < al.Count; i++)
{
    sum = sum + (double)al[i];
    listBox1.Items.Add(al[i]);
}

label1.Text = sum.ToString(); // sum = 293.597

 


15. Search of position of first occurrence of item in array ArrayList. Method IndexOf().

To determine of position of first occurrence the given item in array is used function IndexOf(). Function returns position of first occurrence. If the item is not in array then function returns -1.

Function has three overloaded realizations.

Suppose that the following array is formed.

ArrayList al = new ArrayList();
int index;

al.Add(10);
al.Add(20);
al.Add(30);
al.Add(40);
al.Add(50);
al.Add(40);
al.Add(30); // al = [10, 20, 30, 40, 50, 40, 30]

Example 1. Search the position of first occurrence of item.

index = al.IndexOf(30); // index = 2

Example 2. Search the position from the specified index to the end of array. Function takes two parameters. First parameter – the item that is searched. Second parameter – position from which the search is starting.

index = al.IndexOf(30, 3); // index = 6

Example 3. Search of position of the given item. Function takes three parameters. First parameter – the item that is searched. The second option – a position from which the search starts. The third parameter – the number of characters that are considered during the search.

index = al.IndexOf(30, 3, 2); // index = -1

 


16. How to insert an item in the given position of array ArrayList? Method Insert().

Method Insert() is used to insert an item at a given position. Method takes two parameters. First parameter – item, which is inserted. Second parameter – item position.

Example.

ArrayList al = new ArrayList();
al.Add(10);
al.Add(20);
al.Add(50); // al = [10, 20, 50]

al.Insert(0, 5); // al = [5, 10, 20, 50]
al.Insert(3, 60); // al = [5, 10, 20, 50, 60]

// error - position the outside the array
// al.Insert(8, 30);

 


17. How to insert another array into array ArrayList at the given position? Method InsertRange().

Method InsertRange() allows to insert a subarray into array of type ArrayList. Method takes two parameters. First parameter – position of inserting. Second parameter – array that is inserted.

Example.

ArrayList al = new ArrayList();
ArrayList al2 = new ArrayList();

al.Add(10);
al.Add(20);
al.Add(50); // al = [10, 20, 50]

al2.Add(30);
al2.Add(40); // al2 = [30, 40]

al.InsertRange(2, al2); // al = [10, 20, 30, 40, 50]

 


18. Determining of last occurrence a given item. Method LastIndexOf().

Method LastIndexOf() finds the position of last occurrence of item. Method searches the position from the end to the begin of array. If the item is not found, the method returns -1.

The method has three overloads.

Example.

ArrayList al = new ArrayList();
al.Add(10);
al.Add(20);
al.Add(30);
al.Add(50);
al.Add(30);
al.Add(20);
al.Add(10); // al = [10, 20, 30, 50, 30, 20, 10]

int index;

// Searching of value 20 from the end of array
index = al.LastIndexOf(20);  // index = 5

// Search the value 30 from position -1 to the begin of array.
index = al.LastIndexOf(30, 1);   // index = -1

// Search the value 10 from position 5, 2 items are considered
index = al.LastIndexOf(10, 5, 2); // index = -1

 


19. How delete the item from array ArrayList from a given position? Method RemoveAt().

To delete the item in ArrayList from a given position, you need to use method RemoveAt().

Example.

ArrayList al = new ArrayList();
int index;

al.Add(10);
al.Add(20);
al.Add(30);
al.Add(40);
al.Add(50); // al = [10, 20, 30, 40, 50]

index = 3;
al.RemoveAt(index); // al = [10, 20, 30, 50]

 


20. Deleting the range of values, given by indexes. Method RemoveRange().

To delete several items from the array ArrayList by selected range, you need to use method RemoveRange(). Method takes two parameters. First parameter – index, from which deleting is begun. Second parameter – number of characters, that are deleted.

ArrayList al = new ArrayList();
al.Add(10);
al.Add(20);
al.Add(30);
al.Add(40);
al.Add(50); // al = [10, 20, 30, 40, 50]
al.RemoveRange(1, 2); // al = [10, 40, 50]

 


21. Reverse the array ArrayList. Method Reverse().

Function Reverse() reverses the array of type ArrayList in the backward order. Function has two overloads.

Example1. Using the function Reverse().

ArrayList al = new ArrayList();
al.Add(10);
al.Add(20);
al.Add(30);
al.Add(40);
al.Add(50); // al = [10, 20, 30, 40, 50]
al.Reverse(); // al = [50, 40, 30, 20, 10]

Example 2. Function Reverse() which takes two parameters. First parameter – position number, from which reverse is realized. Second parameter – number of items, that are processed.

ArrayList al = new ArrayList();
al.Add(10);
al.Add(20);
al.Add(30);
al.Add(40);
al.Add(50);       // al = [10, 20, 30, 40, 50]
al.Reverse(2, 3); // al = [10, 20, 50, 40, 30]

 


22. Copying the items of array ArrayList into another array. Method SetRange().

Method SetRange() copies items of array ArrayList into another array ArrayList by changing them.

Example.

ArrayList al = new ArrayList();
ArrayList al2 = new ArrayList();

al.Add(10);
al.Add(20);
al.Add(30);
al.Add(40);
al.Add(50); // al = [10, 20, 30, 40, 50]

al2.Add(5);
al2.Add(15);
al2.Add(25); // al2 = [5, 15, 25]

al.SetRange(2, al2); // al = [10, 20, 5, 15, 25]

 


23. How sort the array of type ArrayList? Method Sort().

Method Sort() sorts the items of array ArrayList using a realization of interface IComparable.

Example.

ArrayList al = new ArrayList();
al.Add(5);
al.Add(-2);
al.Add(10);
al.Add(0);
al.Add(-8); // al = [5, -2, 10, 0, -8]
al.Sort(); // al = [-8, -2, 0, 5, 10]

 


24. How to copy data from ArrayList to the ListBox control?

Example 1.

An example that copies data from ArrayList into the ListBox control for displaying it on the form.

ArrayList al = new ArrayList();
al.Add(5);
al.Add(-2);
al.Add(10);
al.Add(0);
al.Add(-8); // al = [5, -2, 10, 0, -8]

listBox1.Items.Clear();
for (int i = 0; i < al.Count; i++)
{
    listBox1.Items.Add(al[i]);
}

Example 2. Copying the structure BOOK (see p.8).

Let is given the following structure BOOK

struct BOOK
{
    public string Author;
    public string Title;
    public int year;
    public double price;
};

To add data use the following code:

BOOK b1 = new BOOK();
ArrayList al = new ArrayList(25);

// create the first book
b1.Author = "Author-1";
b1.Title = "Title-1";
b1.year = 1998;
b1.price = 90.95;

al.Add(b1); // add book to the array

// create the second book
b1.Author = "Author-02";
b1.Title = "Title-02";
b1.year = 1980;
b1.price = 130.94;

al.Add(b1);

// converting the book
BOOK[] ar = (BOOK[])al.ToArray(typeof(BOOK));

// delete all items from array
al.Clear(); // al.Count = 0

listBox1.Items.Clear();
for (int i = 0; i < al.Count; i++)
{
  listBox1.Items.Add(ar[i].Author + " - " +
                     ar[i].Title + " - " +
                     ar[i].year.ToString() + " - " +
                     ar[i].price.ToString());
}