C# dynamic arrayList


Release date:2023-09-05 Update date:2023-10-13 Editor:admin View counts:317

Label:

C# dynamic arrayList

A dynamic arrayList represents an ordered collection of objects thatcan be indexed separately. It can basically replace an array. However, unlike an array, you can use the index to add and remove items at a specified location, and the dynamic array automatically resizes it. It also allows dynamic memory allocation, adding, searching, and sorting items in the list.

Methods and properties of the ArrayList class

The following table lists some ArrayList class common properties:

Attribute

Description

Capacity

Gets or sets the number of elements that an ArrayList can contain.

Count

Gets the number of elements actually contained in the ArrayList.

IsFixedSize

Gets a value indicating whether the ArrayList has a fixed size.

IsReadOnly

Gets a value indicating whether the ArrayList is read-only.

IsSynchronized

Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).

Item [Int32]

Gets or sets the element at the specified index.

SyncRoot

Gets an object to access the ArrayList synchronously.

The following table lists some ArrayList class common methods:

Serial number

Method name & description

1

Public virtual int Add (object value); add an object at the end of the ArrayList.

2

Public virtual void AddRange (ICollection c); add the element of ICollectionat the end of the ArrayList.

3

Public virtual void Clear (); removes all elements from the ArrayList.

4

Public virtual bool Contains (object item); determines whether an element isin ArrayList.

5

Public virtual ArrayList GetRange (int index, int count); returns an ArrayList that represents a subset of the elements in the source ArrayList.

6

Public virtual int IndexOf (object); returns the index at which a value first appears in ArrayList, starting at zero.

7

Public virtual void Insert (int index, object value); insert an element at the specified index of ArrayList.

8

Public virtual void InsertRange (int index, ICollection c); inserts an element of a collection at the specified index of ArrayList.

9

Public virtual void Remove (object obj); removes the first occurrence of thespecified object from the ArrayList.

10

Public virtual void RemoveAt (int index); removes the element at the specified index of the ArrayList.

11

Public virtual void RemoveRange (int index, int count); removes a range of elements from the ArrayList.

12

Public virtual void Reverse (); reverses the order of elements in ArrayList.

13

Public virtual void SetRange (int index, ICollection c); copy the elements of a collection to a range of elements in ArrayList.

14

Public virtual void Sort (); sorts the elements in the ArrayList.

15

Public virtual void TrimToSize (); sets the capacity to the actual number ofelements in the ArrayList.

Example

The following example demonstrates the concept of dynamic arrayList:

Example

using System;
using System.Collections;

namespace CollectionApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();

            Console.WriteLine("Adding some numbers:");
            al.Add(45);
            al.Add(78);
            al.Add(33);
            al.Add(56);
            al.Add(12);
            al.Add(23);
            al.Add(9);

            Console.WriteLine("Capacity: {0} ", al.Capacity);
            Console.WriteLine("Count: {0}", al.Count);

            Console.Write("Content: ");
            foreach (int i in al)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.Write("Sorted Content: ");
            al.Sort();
            foreach (int i in al)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Adding some numbers:
Capacity: 8
Count: 7
Content: 45 78 33 56 12 23 9
Content: 9 12 23 33 45 56 78

Powered by TorCMS (https://github.com/bukun/TorCMS).