Learn About ArrayList in C#: Part 1

1. What is ArrayList

ArrayList is a dynamic array that can be resized when we declare indefinite number of elements in the array. 
ArrayList provides a great advantage in the management of memory because it allocates more when need arises, and to avoid waste when given a fixed number of elements is defined.

2. Some important methods of ArrayList

  • Item (): set and access components in an array list at the specified position.
  • Add (): add an object in the array list.
  • Clear (): remove all components of the ArrayList.
  • Clone (): make a copy.
  • Contains () to check whether a certain element exists in the array list or not.
  • CopyTo () method overloaded array used to copy a list to a one-dimensional array.
  • IndexOf (): Returns the index position of the first value.
  • Insert (): insert an element into the ArrayList.
  • Sort (): Sort and ArrayList elements.
  • ToArray () to copy the elements of the ArrayList to a new array.

3. Add elements in the ArrayList


Looking at the code above we can see we created one ArrayList list without a predetermined number of fixed elements. Whenever we need to add a new element we use the add () method. In the array we have a default attribute "count" to count the number of elements in the list. This attribute will be updated automatically each time the add () is called. And we can easily access all of the elements of the ArrayList through function foreach (). In order to browse through all the elements in the list, we can traverse the list using the foreach() function and display each element of the result to the console.

In the example above we have not declared a data type for each element of the list. If we want to create one list of integers, then we just replace the preceding string to integer. When needed, the value can be fetched using the foreach function to find all the integers in the list.

So is it possible to create a list of many different data types? You can create one list that has just string, int and char, float, but a problem exists when the elements are of different data types. In that case, how to use foreach () to access it? To overcome this situation, we can use the variable var to access elements in the list that does not care what the data is.

To learn more you can see in the example below: 


4. Connect two ArrayList

ArrayList supports the AddRange() method to easily connect two ArrayList lists. More precisely the method adds the elements of the second list to the end of first list as in the following.

For example:


In the output above e we can see that the first list is appended with the element of list2, new elements are added at the end of list1. And the element of list2 does not change.

5. Extract one 
ArrayList from another

We have seen in the example above how to assemble one ArrayList into another ArrayList. At this point we are wondering whether we can do the reverse or not? 

This means that we can create a new list of elements from the original list. To better understand this case we look at the example below. 

Initially we have a list of names, list1, with four elements. We can create a new list as list2 using the GetRange() method. The GetRange() method returns a subset of elements from the original list list1. GetRange has 2 arguments. The first argument is the starting position of the element in list1. Note that the position of the element in the list is calculated from 0. The second argument indicates the number of elements in the range. It is worth noticing one more point; that after creating the new list list2, there is no change in the original ArrayList. 


6. Remove all the elements in the ArrayList

It's easy to remove all the elements in the list by the Clear() method. After using the Clear() method, 
ArrayList.count will return 0 and all elements of the list will be cleared, restoring the initial state of the list.

From my Site: http://microsofttech.net/lap-trinh/tim-hieu-ve-arraylist-trong-c-p-1.html