Advertisement
Constructors | Description |
---|---|
ArrayList() | This constructor of ArrayList creates an empty ArrayList and has the default initial capacity. The capacity of an ArrayList is the number of elements it can hold, as the new elements are added to the ArrayList, its capacity is automatically increased. |
ArrayList(ICollection) | This constructor creates an ArrayList initialized with the elements of an ICollection. |
ArrayList(int capacity) | This constructor initializes created an ArrayList that is empty and has the specified initial capacity.. |
Properties | Description |
---|---|
Capacity | This property is used to get or set the total number of elements an ArrayList can hold. |
Count | This property gives total number of elements in an ArrayList. |
IsFixedSize | This property determines whether an ArrayList has a fixed size or not. |
IsReadOnly | This property determines whether an ArrayList is read-only or not. |
IsSynchronzied | This property determines whether an ArrayList is thread-safe or not. |
Item[int index] | This property gets or sets an item of ArrayList at a specified index. |
Methods | Description |
---|---|
int Add(Object ob) | This method adds the specified object at the end of an ArrayList and returns its index. |
bool Contains(Object o) | This method checks if ArrayList contains a specific Object. |
void Clear() | This method removes all of the elements from the ArrayList. |
IEnumerator GetEnumerator() | This method returns to enumerate the entire ArrayList. |
int IndexOf(Object o) | This method searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList. |
int LastIndexOf(Object o) | This method searches for the specified Object and returns the zero-based index of the last occurrence within the entire ArrayList. |
void Remove(Object o) | This method removes the first occurrence of an Object from the ArrayList, if it is present. |
void RemoveAt(int index) | This method removes an element at a specified index in an ArrayList. |
Object[] ToArray() | This method returns an Object array containing all the elements of an ArrayList. |
void Sort() | This method sorts an ArrayList. |
void Reverse() | This method reverses the order of the elements of an ArrayList. |
Advertisement
//C# Example of ArrayList
using System;
using System.Collections;
class ArrayList1
{
public static void Main(String[] ar)
{
//Creating an ArrayList
ArrayList arr= new ArrayList();
//Printing the initial capacity of an ArrayList
Console.WriteLine("Initial capacity of ArrayList = "+ arr.Capacity);
//Adding elements to an ArrayList by Add() method
arr.Add("C");
arr.Add('A');
arr.Add(14.4f);
arr.Add("F");
arr.Add('x');
arr.Add(1);
//Printing the elements of an ArrayList, using for loop
Console.Write("ArrayList after adding objects: ");
for(int i=0;i<arr.Count;i++)
Console.Write(arr[i]+ " ");
//Printing the total number of elements in an ArrayList.
Console.WriteLine("\nSize of ArrayList = "+ arr.Count);
//Removing an element at the 4th index of ArrayList
arr.RemoveAt(4);
//Removing an element "1" of the ArrayList
arr.Remove("1");
//Printing the elements of an ArrayList, using foreach loop
Console.Write("An updated ArrayList: ");
for(int i = 0; i<arr.Count; i++)
Console.Write(arr[i] + " ");
//Printing the total number of elements in an ArrayList.
Console.WriteLine("\nSize of ArrayList = "+ arr.Count);
//Printing the current capacity of an ArrayList
Console.WriteLine("Current capacity of ArrayList = "+ arr.Capacity);
}
}
Initial capacity of ArrayList = 0
ArrayList after adding objects: C A 14.4 F x 1
Size of ArrayList = 6
An updated ArrayList: C A 14.4 F 1
Size of ArrayList = 5
Current capacity of ArrayList = 8
//C# Example of ArrayList
using System;
using System.Collections;
class ArrayList1
{
public static void Main(String[] ar)
{
//Creating an ArrayList
ArrayList arr= new ArrayList();
//Printing the initial capacity of an ArrayList
Console.WriteLine("Initial capacity of ArrayList = "+ arr.Capacity);
//Adding elements to an ArrayList by Add() method
arr.Add("C");
arr.Add('A');
arr.Add(14.4f);
arr.Add("F");
arr.Add('x');
arr.Add(1);
//Printing the elements of an ArrayList, using foreach loop
Console.Write("ArrayList after adding objects: ");
foreach(Object ob in arr)
Console.Write(ob + " ");
//Printing the total number of elements in an ArrayList.
Console.WriteLine("\nSize of ArrayList = "+ arr.Count);
}
}
Initial capacity of ArrayList = 0
ArrayList after adding objects: C A 14.4 F x 1
Size of ArrayList = 6
//C# Example of creating a new ArrayList created from an existing one
using System;
using System.Collections;
class ArrayList1
{
public static void Main(String[] ar)
{
//Creating an ArrayList
ArrayList array1 = new ArrayList();
//Adding elements to an ArrayList by Add() method
array1.Add(4);
array1.Add(1);
array1.Add(5);
array1.Add(2);
array1.Add(3);
//Printing the elements of an ArrayList, using foreach loop
Console.Write("ArrayList after adding objects: ");
for(int i = 0; i<array1.Count; i++)
Console.Write(array1[i] + " ");
//Printing the total number of elements in an ArrayList.
Console.WriteLine("\nSize of ArrayList = "+ array1.Count);
Console.WriteLine("Creating a new ArrayList created from an existing one");
//Calling a constructor of ArrayList
//Which creates a new ArrayList, initialized from an existing one
ArrayList array2= new ArrayList(array1);
//Printing the elements of an ArrayList, using foreach loop
Console.Write("Contents of the new ArrayList: ");
for(int i = 0; i<array2.Count; i++)
Console.Write(array2[i] + " ");
//Printing the total number of elements in an ArrayList.
Console.WriteLine("\nSize of the new ArrayList = "+ array2.Count);
}
}
ArrayList after adding objects: 4 1 5 2 3
Size of ArrayList = 5
Creating a new ArrayList created from an existing one
Contents of the new ArrayList: 4 1 5 2 3
Size of the new ArrayList = 5
//C# Converting an ArrayList to an Object array using the ToArray() method.
using System;
using System.Collections;
class ArrayList1
{
public static void Main(String[] ar)
{
//Creating an ArrayList
ArrayList array1 = new ArrayList();
//Adding int elements to an ArrayList by Add() method
array1.Add(4);
array1.Add(1);
array1.Add(5);
array1.Add(2);
array1.Add(3);
//Printing the elements of an ArrayList, using for loop with Count property
Console.Write("ArrayList after adding objects: ");
for(int i = 0; i<array1.Count; i++)
Console.Write(array1[i] + " ");
//Printing the total number of elements in an ArrayList.
Console.WriteLine("\nSize of ArrayList = "+ array1.Count);
//Converting an ArrayList to an Object array.
//By calling the ToArray() method
Object[] obArr = array1.ToArray();
Console.WriteLine("Creating an Object Array created from ArrayList - ");
//Iterating over the elements of Object array, using foreach loop
Console.Write("The elements of Object array: ");
for(int i = 0; i<obArr.Length; i++)
Console.Write(obArr[i] + " ");
}
}
ArrayList after adding objects: 4 1 5 2 3
Size of ArrayList = 5
Creating an Object Array created from ArrayList -
The elements of Object array: 4 1 5 2 3
//C# Using the Sort() method of ArrayList
using System;
using System.Collections;
class ArrayList1
{
public static void Main(String[] ar)
{
//Creating an ArrayList
ArrayList array1 = new ArrayList();
//Adding int elements to an ArrayList by Add() method
array1.Add(4);
array1.Add(1);
array1.Add(5);
array1.Add(10);
array1.Add(3);
//Printing the elements of an ArrayList, using foreach loop
Console.Write("ArrayList after adding objects: ");
for(int i = 0; i<array1.Count; i++)
Console.Write(array1[i] + " ");
//Printing the total number of elements in an ArrayList.
Console.WriteLine("\nSize of ArrayList = "+ array1.Count);
//Sorting the ArrayList by calling the ToSort() method of ArrayList
array1.Sort();
Console.Write("Sorted ArrayList: ");
//Iterating over the elements of sorted ArrayList, using foreach loop
for(int i = 0; i<array1.Count; i++)
Console.Write(array1[i] + " ");
}
}
ArrayList after adding objects: 4 1 5 10 3
Size of ArrayList = 5
Sorted ArrayList: 1 3 4 5 10
//C# Using the Reverse() method of ArrayList
using System;
using System.Collections;
class ArrayList1
{
public static void Main(String[] ar)
{
//Creating an ArrayList
ArrayList array1 = new ArrayList();
//Adding int elements to an ArrayList by Add() method
array1.Add("4");
array1.Add("A");
array1.Add("x");
array1.Add("E");
array1.Add('r');
//Printing the elements of an ArrayList, using foreach loop
Console.Write("ArrayList after adding objects: ");
for(int i = 0; i<array1.Count; i++)
Console.Write(array1[i] + " ");
//Printing the total number of elements in an ArrayList.
Console.WriteLine("\nSize of ArrayList = "+ array1.Count);
//Reversing the order of an ArrayList by calling the Reverse() method of ArrayList
array1.Reverse();
Console.Write("Reversed ArrayList: ");
//Iterating over the elements of our reversed ArrayList, using foreach loop
for(int i = 0; i<array1.Count; i++)
Console.Write(array1[i] + " ");
}
}
ArrayList after adding objects: 4 A x E r
Size of ArrayList = 5
Reversed ArrayList: r E x A 4
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement