VB.Net - ArrayList
It represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. However, unlike array, you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, adding, searching and sorting items in the list.
Properties and Methods of the ArrayList Class
The following table lists some of the commonly used properties of the ArrayList class
1 | Capacity Gets or sets the number of elements that the ArrayList can contain. |
2 | Count Gets the number of elements actually contained in the ArrayList. |
3 | IsFixedSize Gets a value indicating whether the ArrayList has a fixed size. |
4 | IsReadOnly Gets a value indicating whether the ArrayList is read-only. |
5 | Item Gets or sets the element at the specified index. |
The following table lists some of the commonly used methods of the ArrayList class
1 | Public Overridable Function Add [value As Object] As Integer Adds an object to the end of the ArrayList. |
2 | Public Overridable Sub AddRange [c As ICollection] Adds the elements of an ICollection to the end of the ArrayList. |
3 | Public Overridable Sub Clear Removes all elements from the ArrayList. |
4 | Public Overridable Function Contains [item As Object] As Boolean Determines whether an element is in the ArrayList. |
5 | Public Overridable Function GetRange [index As Integer, count As Integer ] As ArrayList Returns an ArrayList, which represents a subset of the elements in the source ArrayList. |
6 | Public Overridable Function IndexOf [value As Object] As Integer Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it. |
7 | Public Overridable Sub Insert [index As Integer, value As Object] Inserts an element into the ArrayList at the specified index. |
8 | Public Overridable Sub InsertRange [index As Integer, c As ICollection] Inserts the elements of a collection into the ArrayList at the specified index. |
9 | Public Overridable Sub Remove [obj As Object ] Removes the first occurrence of a specific object from the ArrayList. |
10 | Public Overridable Sub RemoveAt [index As Integer] Removes the element at the specified index of the ArrayList. |
11 | Public Overridable Sub RemoveRange [index As Integer, count As Integer] Removes a range of elements from the ArrayList. |
12 | Public Overridable Sub Reverse Reverses the order of the elements in the ArrayList. |
13 | Public Overridable Sub SetRange [index As Integer, c As ICollection ] Copies the elements of a collection over a range of elements in the ArrayList. |
14 | Public Overridable Sub Sort Sorts the elements in the ArrayList. |
15 | Public Overridable Sub TrimToSize Sets the capacity to the actual number of elements in the ArrayList. |
Example
The following example demonstrates the concept
Sub Main[] Dim al As ArrayList = New ArrayList[] Dim i As Integer 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: "] For Each i In al Console.Write["{0} ", i] Next i Console.WriteLine[] Console.Write["Sorted Content: "] al.Sort[] For Each i In al Console.Write["{0} ", i] Next i Console.WriteLine[] Console.ReadKey[] End Sub End ModuleWhen the above code is compiled and executed, it produces the following result
Adding some numbers: Capacity: 8 Count: 7 Content: 45 78 33 56 12 23 9 Content: 9 12 23 33 45 56 78Useful Video Courses
VB.NET Masterclass: Learn Visual Basic and VBScript
63 Lectures 4 hours
Frahaan Hussain
AutoCAD Programming using VB.NET - Hands On!
103 Lectures 12 hours
Arnold Higuit
Developing AutoCAD Plugins using VB.NET with Windows Forms
60 Lectures 9.5 hours
Arnold Higuit
Programming AutoCAD to Excel using VB.NET - Hands On!
97 Lectures 9 hours
Arnold Higuit