After reading this article, you will learn about
- ArrayList.
- Uses of an ArrayList.
- How to access the elements in an ArrayList.
- Types of used in Methods in an ArrayList.
The first and foremost thing to remember about an array is that it does not have a fixed size; it grows, as required, always.
- Use Add (object) to add an object to the end of the ArrayList.
- Use [] to access the elements in the ArrayList.
- Use TrimToSize() to reduce the size to fit the number of elements in the ArrayList.
- User clears to remove all the elements.
Actually, ArrayList can solve the main disadvantage of an array, which is that we must know the capacity of the data structure, when you instantiate the array by providing a data structure that behaves like an array but can grow, as required.
As the elements are added to an ArrayList object, the capacity is automatically increased. An ArrayList object initially allocates 16 elements. When we add a seventeenth element, the ArrayList expands to 32 elements.
How to access the elements in an ArrayList
We can access the elements in an ArrayList object in the same way as you access the arrays. We can also use ArrayList methods to add the elements to or remove the elements from an ArrayList.
To decrease the capacity of an ArrayList, we can call TrimToSize method.
Use Add method to add the items to an ArrayList.
We can also do Foreach to iterate over the items in an ArrayList.
Note
ArrayList elements are the objects such as System. Object. Thus, when we retrieve the elements from the list, we must do some conversions.
Types of methods used in an ArrayList
Add
Add an object at the end of the ArrayList.
Remove
Remove the first appeared object from the ArrayList.
Clear
Removes all the elements from the ArrayList.
Insert
Inserts an element into the ArrayList at the specified index.
TrimToSize
Sets the capacity to the actual number of the elements in the ArrayList.
Sort
Sorts the elements in the ArrayList.
Reverse
Reverse the elements in the ArrayList.
The sample code given shows how to use an ArrayList.
- We must include the system.collection namespace, as shown below.
- using System;
- using System.Collections;
- The ArrayList is initialized without specifying the size, because it may grow, when needed.
Name of Arraylist is theAnimals, as shown below.
Code
- public class Zoo
- {
- private ArrayList theAnimals;
- public ArrayList ZooAnimals
- {
- get
- {
- return theAnimals;
- }
- }
-
-
- public Animal this[int i]
- {
- get
- {
- return (Animal) theAnimals[i];
- }
- set {
- theAnimals[i] = value;
- }
- }
- public Zoo()
- {
- theAnimals = new ArrayList();
- }
- }
- The Add and Insert methods are used to add the elements to the array. This is the main difference between an Array and an ArrayList, as shown below.
- public class Guardian
- {
- static void Main(string[] args)
- {
- Zoo myzoo = new Zoo();
- myzoo.ZooAnimals.Add(new Lion());
- myzoo.ZooAnimals.Add(new Elephant());
- myzoo.ZooAnimals.Insert(1, new Lion());
-
- Animal a = myzoo [0];
- myzoo[1] = new Tiger();
-
- }
-
- }
- We can access the elements of the ArrayList by using the Index Operator[], as shown below.
- Animal a = myzoo[0];
- myzoo [1] = new Tiger();
public abstract class Animal{
abstract public void Eat();
}
public class Lion :Animal{
public override void Eat();
}
public class Elephant :Animal{
public override void Eat();
}
public class Tiger :Animal{
public override void Eat();
}
Thanks for reading my article. I hope you like it.