0% found this document useful (0 votes)
3 views5 pages

VB.NET ArrayList Usage Guide

Uploaded by

Aushu Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

VB.NET ArrayList Usage Guide

Uploaded by

Aushu Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ArrayList

This [Link] collection is a dynamic array—it resizes to


fit new elements. An array type offers superior
performance. But the ArrayList is sized automatically
by built-in code.

ArrayList has many functions that help manage


a linear collection. We add objects, we remove
and insert things, and we count the number of
elements.
Add Example
First the Add() subroutine is often used. It
appends the object argument to the end of the
internal ArrayList data structure. This program
adds three elements to the ArrayList.
Module Module1

Sub Main()
' Create a new ArrayList, and add 3 strings
to it.
Dim list As New ArrayList
[Link]("One")
[Link]("Two")
[Link]("Three")
[Link]([Link])
End Sub

End Module
Parameter
It is possible and often useful to receive
an ArrayList as a parameter to a Sub. We can
specify it as a parameter with "As ArrayList." The
syntax is simple.
Module Module1

Sub Main()
Dim list As New ArrayList
[Link](5)
[Link](7)
Example(list)
End Sub
Private Sub Example(ByVal list As ArrayList)
For Each num As Integer In list
[Link](num)
Next
End Sub

End Module

Add Range
It is possible to add a range of elements from
one ArrayList onto the end of
another ArrayList. To do this, please consider
using the AddRange Sub.
Module Module1

Sub Main()
Dim list1 As New ArrayList
[Link](5)
[Link](7)
Dim list2 As New ArrayList
[Link](10)
[Link](13)
[Link](list2
For Each num As Integer In list1
[Link](num)
Next
End Sub

End Module

Count, Clear
Often with ArrayList, you will not be sure how
many elements are in the current instance.
Fortunately, the ArrayList offers
the Count property.
Module Module1

Sub Main()
Dim list As New ArrayList
[Link](9)
[Link](10)
[Link]([Link])
[Link]()
[Link]([Link])
End Sub

End Module
Insert, Remove
We show how to use
the Add(), RemoveAt(), Insert(),
and RemoveRange() methods. We have already
seen the Add() method in the first example
Module Module1

Sub Main()
' Create an ArrayList and add three strings to it.
Dim list As New ArrayList
[Link]("Dot")
[Link]("Net")
[Link]("Perls")
' Remove a string.
[Link](1)
' Insert a string.
[Link](0, "Carrot")
' Remove a range.
[Link](0, 2)
' Display.
For Each str As String In list
[Link](str)
Next
End Sub

End Module

Try Cast
In the ArrayList, elements are not directly stored
with a type. Instead they are accessed through the
Object base type. To cast an Object to a more
derived type, use TryCast.
Module Module1

Sub Main()
' Create a new ArrayList.
Dim list As New ArrayList
[Link]("man")
[Link]("woman")
[Link]("plant")
' Loop over the ArrayList with a For-loop.
For i As Integer = 0 To [Link] - 1
' Cast to a string.
Dim str As String = TryCast([Link](i), String)
[Link](str)
Next i
End Sub

End Module

GetRange
Here we extract one part of an ArrayList into
another. To do this, please use the GetRange method
on the original ArrayList instance.
Module Module1

Sub Main()
' Create new ArrayList.
Dim list1 As New ArrayList
[Link]("fish")
[Link]("amphibian")
[Link]("bird")
[Link]("plant")
' Create a new ArrayList.
' ... Fill it with the range from the first one.
Dim list2 As New ArrayList
list2 = [Link](2, 2)
' Loop over the elements.
For Each str As String In list2
[Link](str)
Next
End Sub

End Module

We examined the ArrayList type—this is a


core class in .NET. It is harder to use in [Link] due
to the casting syntax required, but older programs
often need it.

You might also like