Add different data types to ArrayList
Imports System.Collections
public class AddElementsToArrayLista
public Shared Sub Main
Dim alist1 As New ArrayList()
alist1.Add("Hello") "Add a System.String
alist1.Add(714) "Add a System.Int32
alist1.Add(New DateTime(2003, 1, 1)) "Add a System.DateTime
alist1.Add(#1/1/2003#) "Add a System.DateTime
alist1.Add(4.5) "Add a System.Double
alist1.Add(4.5F) "Add a System.Single
Dim o As Object
For Each o In alist1
Console.WriteLine(o & ": " & o.GetType().ToString())
Next
End Sub
End class
Hello: System.String
714: System.Int32
01/01/2003: System.DateTime
01/01/2003: System.DateTime
4.5: System.Double
4.5: System.Single
Add user defined object to ArrayList
Imports System.Collections
Structure Person
Dim strLastName As String
Dim strFirstName As String
Dim strPhone As String
Dim strEMail As String
End Structure
public class Test
public Shared Sub Main
Dim alPersons As New ArrayList
Dim udtPerson As New Person
"Add the first person.
With udtPerson
.strLastName = "S"
.strFirstName = "J"
.strPhone = "5"
.strEMail = "j@s.ru"
End With
alPersons.Add(udtPerson)
"Add the second person.
With udtPerson
.strLastName = "J"
.strFirstName = "S"
.strPhone = "5"
.strEMail = "s@s.ru"
End With
alPersons.Add(udtPerson)
"Create the third person.
With udtPerson
.strLastName = "J"
.strFirstName = "K"
.strPhone = "5"
.strEMail = "k@s.ru"
End With
"Insert the third person, but first check if they already exists.
If Not alPersons.Contains(udtPerson) Then
alPersons.Insert(1, udtPerson)
End If
"Remove the first person.
alPersons.RemoveAt(0)
"Display the array list values.
Console.WriteLine("The array list contains " & alPersons.Count & " elements.")
For Each udtPerson In alPersons
Console.WriteLine("NAME: " & udtPerson.strFirstName & " " & udtPerson.strLastName)
Next udtPerson
End Sub
End class
The array list contains 2 elements.
NAME: K J
NAME: S J
Convert ArrayList to array
Imports System.Collections
public class Test
public Shared Sub Main
Dim array_list As New ArrayList
array_list.Add("A")
array_list.Add("B")
array_list.Add("C")
" Array object of objects.
Dim astring_array As Array
astring_array = array_list.ToArray()
End Sub
End class
Remove element in an ArrayList by index
Imports System.Collections
Structure Person
Dim strLastName As String
Dim strFirstName As String
Dim strPhone As String
Dim strEMail As String
End Structure
public class Test
public Shared Sub Main
Dim alPersons As New ArrayList
Dim udtPerson As New Person
"Add the first person.
With udtPerson
.strLastName = "S"
.strFirstName = "J"
.strPhone = "5"
.strEMail = "j@s.ru"
End With
alPersons.Add(udtPerson)
"Add the second person.
With udtPerson
.strLastName = "J"
.strFirstName = "S"
.strPhone = "5"
.strEMail = "s@s.ru"
End With
alPersons.Add(udtPerson)
"Create the third person.
With udtPerson
.strLastName = "J"
.strFirstName = "K"
.strPhone = "5"
.strEMail = "k@s.ru"
End With
"Insert the third person, but first check if they already exists.
If Not alPersons.Contains(udtPerson) Then
alPersons.Insert(1, udtPerson)
End If
"Remove the first person.
alPersons.RemoveAt(0)
"Display the array list values.
Console.WriteLine("The array list contains " & alPersons.Count & " elements.")
For Each udtPerson In alPersons
Console.WriteLine("NAME: " & udtPerson.strFirstName & " " & udtPerson.strLastName)
Next udtPerson
End Sub
End class
The array list contains 2 elements.
NAME: K J
NAME: S J
Use ArrayList.Contains to check if a object already exists
Imports System.Collections
Structure Person
Dim strLastName As String
Dim strFirstName As String
Dim strPhone As String
Dim strEMail As String
End Structure
public class Test
public Shared Sub Main
Dim alPersons As New ArrayList
Dim udtPerson As New Person
"Add the first person.
With udtPerson
.strLastName = "S"
.strFirstName = "J"
.strPhone = "5"
.strEMail = "j@s.ru"
End With
alPersons.Add(udtPerson)
"Add the second person.
With udtPerson
.strLastName = "J"
.strFirstName = "S"
.strPhone = "5"
.strEMail = "s@s.ru"
End With
alPersons.Add(udtPerson)
"Create the third person.
With udtPerson
.strLastName = "J"
.strFirstName = "K"
.strPhone = "5"
.strEMail = "k@s.ru"
End With
"Insert the third person, but first check if they already exists.
If Not alPersons.Contains(udtPerson) Then
alPersons.Insert(1, udtPerson)
End If
"Remove the first person.
alPersons.RemoveAt(0)
"Display the array list values.
Console.WriteLine("The array list contains " & alPersons.Count & " elements.")
For Each udtPerson In alPersons
Console.WriteLine("NAME: " & udtPerson.strFirstName & " " & udtPerson.strLastName)
Next udtPerson
End Sub
End class
The array list contains 2 elements.
NAME: K J
NAME: S J
Use ArrayList to store objects
Option Strict On
Imports System
Imports System.Collections
Public Class Employee
Private myEmpID As Integer
Public Sub New(ByVal empID As Integer)
Me.myEmpID = empID
End Sub "New
Public Overrides Function ToString( ) As String
Return myEmpID.ToString( )
End Function "ToString
Public Property EmpID( ) As Integer
Get
Return myEmpID
End Get
Set(ByVal Value As Integer)
myEmpID = Value
End Set
End Property
End Class "Employee
Class Tester
Shared Sub Main( )
Dim empArray As New ArrayList( )
Dim intArray As New ArrayList( )
Dim i As Integer
For i = 0 To 4
empArray.Add(New Employee(i + 100))
intArray.Add((i * 5))
Next i
For Each i In intArray
Console.Write("{0} ", i.ToString( ))
Next i
Console.WriteLine(ControlChars.Lf)
Dim e As Employee
For Each e In empArray
Console.Write("{0} ", e.ToString( ))
Next e
Console.WriteLine(ControlChars.Lf)
Console.WriteLine("empArray.Capacity: {0}", empArray.Capacity)
End Sub
End Class
0 5 10 15 20
100 101 102 103 104
empArray.Capacity: 8
Use DirectCast to convert ArrayList to array
Imports System.Collections
public class Test
public Shared Sub Main
Dim array_list As New ArrayList
array_list.Add("A")
array_list.Add("B")
array_list.Add("C")
" Array of strings.
Dim string_array() As String
string_array = DirectCast(array_list.ToArray(GetType(String)), String())
End Sub
End class
Use For Each to loop through the ArrayList
Imports System.Collections
Structure Person
Dim strLastName As String
Dim strFirstName As String
Dim strPhone As String
Dim strEMail As String
End Structure
public class Test
public Shared Sub Main
Dim alPersons As New ArrayList
Dim udtPerson As New Person
"Add the first person.
With udtPerson
.strLastName = "S"
.strFirstName = "J"
.strPhone = "5"
.strEMail = "j@s.ru"
End With
alPersons.Add(udtPerson)
"Add the second person.
With udtPerson
.strLastName = "J"
.strFirstName = "S"
.strPhone = "5"
.strEMail = "s@s.ru"
End With
alPersons.Add(udtPerson)
"Create the third person.
With udtPerson
.strLastName = "J"
.strFirstName = "K"
.strPhone = "5"
.strEMail = "k@s.ru"
End With
"Insert the third person, but first check if they already exists.
If Not alPersons.Contains(udtPerson) Then
alPersons.Insert(1, udtPerson)
End If
"Remove the first person.
alPersons.RemoveAt(0)
"Display the array list values.
Console.WriteLine("The array list contains " & alPersons.Count & " elements.")
For Each udtPerson In alPersons
Console.WriteLine("NAME: " & udtPerson.strFirstName & " " & udtPerson.strLastName)
Next udtPerson
End Sub
End class
The array list contains 2 elements.
NAME: K J
NAME: S J