ArrayList is a generic data type used in C sharp for storing items like other containers. When we are not sure about the size of the array, we use an ArrayList instead of a simple array. As this is created dynamically, the size of the ArrayList increases or decreases depending on the requirement of the source code.
ArrayLists are not similar to simple lists, as lists contain the values of different data types at a time. But ArrayList contains the values of the same data type just like simple arrays. This guide will contain some major features of ArrayList regarding the creation, insertion, and removal of items from the ArrayList.
Dissimilarities Between Array and ArrayList
- An array is a fixed-length container whereas ArrayList has no fixed length and is a variable-length data structure.
- The speed of execution of ArrayList is slow as compared to arrays as the operation of resizing ArrayList slows the performance.
To implement an ArrayList in C sharp, we have designed a simple program. First, we will talk about the libraries used in the program. Two basic libraries of C sharp are declared here. We need to declare a class in the source code so class, structures, and other data structures or information regarding data types are present in the “system.collections” library. And the simple ‘system’ header file contains the information regarding the system that helps in the execution.
# Using system.collections;
To create a class ArrayList, inside the class, we have declared ArrayList. The arraylist is created dynamically as we allot the memory allocation at run time. The simple syntax for ArrayList creation contains the ArrayList keyword with the name you want to give to the new arraylist.
The ArrayList is dynamic so we do not need to provide any size of the array. After the ArrayList creation, the next step is to add values to the array. Unlike a simple array, we do not use a FOR loop as we have no specific size to which we iterate the loop. So all the values will be assigned manually through a built-in add() function. This function is called through the ArrayList name with the value in the parameter that we want to enter in the ArrayList.
We have added the values of different types. For example: integer, string, and float values are added. A null value is also. It does not contain any character but that location is somehow reserved. By using ‘null’, you cannot enter another value at that point. When you execute the code, a blank space is obtained.
To access each value of the ArrayList, we used a for each loop that will go to the last element.
To execute the above-mentioned source code, we need a compiler in the Linux operating system. ‘MCS’ is used to compile the code, whereas, for the execution, we use ‘mono’ with the ‘.exe’ extension with the file.
You can check that all the relevant values are displayed but a blank space appeared where we used ‘null’.
Capacity and Count of ArrayList
We can apply many functions on ArrayList just like simple arrays. Similarly, we used two features to count the total elements in an ArrayList and the total capacity of the ArrayList.
Capacity indicates the storage space for the elements. As ArrayList does not have a fixed size, its size can change on every entry of a new item. So to perform a function, we will create a new ArrayList and then add three items to it through the add() function.
The other two items will also be added likewise. Just like the add() function, count and capacity are used. We use the new ArrayList name keyword along with the functions to apply them on the ArrayList.
Now we will execute the program to see the results. As we have entered three items, so the value of the count is 3, and the capacity is shown as 4. As we keep on increasing the number of items in the ArrayList, the capacity of the ArrayList increases automatically.
Remove Elements from the ArrayList
We can remove or delete the items of the ArrayList in many ways. Each approach is used in this example.
Before applying the remove() function, we need to have an ArrayList created with the elements added to it. First, we will use the above approaches to create and enter elements to the new ArrayList. We have added 9 items to the ArrayList.
The total number of items will be demonstrated through the count() function.
Remove by mentioning the item
The first way is to mention the item you want to eliminate from the ArrayList. This function will directly delete the specific element that we mention in the parameter of the remove function.
All functions of ArrayList are operated through the name of the array. That acts as an object for the function call. We use the ‘I’ element in the function. As ‘I’ is present at 2 different locations, both will be searched and removed by the Remove() function. After the removal of elements, the count function will again count the remaining number of items.
Remove by mentioning the index number
The second way deals with providing the index number as a parameter of the removeat() function. This removeat() function fetches the item of the specified index number and then removes it.
We have mentioned the ‘4’ index, so ‘X’ will be removed from the ArrayList. Again the count function will display the number of items left behind.
Remove by mentioning a range of index
We can also remove the number of items collectively by using a single function. We provide a range of index numbers, the starting, and the ending index number for the ArrayList to remove all the items. The function is named as RemoveRange(). This function takes two integer parameters.
We will remove only two items so the range is from 1 to 2. The count() will count the total elements present in the array index after removing the range of items.
Remove all items
To empty the whole ArrayList, we simply use a clear() function that removes all the indexes.
Save the code and then execute it. You will see that each time, count function has displayed the number of items in the ArrayList after each removal function performed.
ArrayList differs from an array in terms of the fixed and free sizes. ArrayList is a free size, we can add elements at run time without mentioning the limit for the numbers. ArrayList is created by using the ‘new’ keyword and we add items to it through a built-in function. The removal of items through ArrayList is followed by 3 methods, the item itself, index number, and index range as well. As, by adding items, the size of the ArrayList increases, on removal it decreases in the same way.
We have implemented some basic examples in Ubuntu by using MCS and mono for compilation and execution purposes.