stu nicholls dot com | menu - Professional dropdown #3

ArrayList

An array list represents an ordered collection of an object that can be indexed individually. It is a dynamic array. It resizes to fit new elements. It is basically an alternative to an array.

Following code is used add the elements to the Array List: Fig. 68 shows the creation of the array list.

Dim arr_lst, arr_lst1 As New ArrayList
Private Sub Array_list_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Array_list.Click
Dim str() As String = {"TCP/IP", "HTML", "PHP", "Android", "ASP.Net"} 'String Array
With arr_lst
'Adding Elements to the Array List One By One Using With 'Statement
.Add("C Programming")
.Add("Visual Basic")
.Add("Data Mining")
.Add("Discrete Maths")
End With
' We Can also Add the Range of Value by AddRange
arr_lst1.AddRange(str)
MsgBox("Array Lists are Created")
End Sub

Fig75



Following code is used for displaying the elements of the Array List:
Fig. 76 shows the message which says for loop being used to display the contents of the array list and Fig. 77 shows the display of elements of array list which was created. Fig. 78 shows the message which says for..each loop is being used for the display of elements of the created array list and Fig. 79 displays the elements of array list.

Dim arr_lst, arr_lst1 As New ArrayList
Private Sub btndisAr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisAr.Click
MsgBox(" Dispalying First Array List using For Loop")
Dim i, j
Dim s As String = ""
For i = 0 To arr_lst.Count - 1
s = s & i + 1 & ": " & arr_lst(i) & ControlChars.CrLf
Next
MsgBox("Elements " & ControlChars.CrLf & s)
MsgBox(" Dispalying Second Array List using For Each Loop")
i = 0
s = ""
For Each j In arr_lst1
i += 1
s = s & i + 1 & ": " & j & ControlChars.CrLf
Next
MsgBox("Elements " & ControlChars.CrLf & s)
End Sub
Fig76



Fig77



Fig78



Fig79



Following is the code for removing elements from the Array List:
When the program is executed it asks through the Input box for the position of the element which you want to delete, which is shown in Fig. 80. Then it will display element which is being deleted both from first and second array list which is shown in Fig. 81. and Fig. 82.

Dim arr_lst, arr_lst1 As New ArrayList
Private Sub btnRemAL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemAL.Click
Dim i As Integer = Integer.Parse(InputBox("Enter the Position to delete : "))
MsgBox("Element to be removed from First Array List is :" & arr_lst.Item(i - 1))
arr_lst.RemoveAt(i - 1)
MsgBox("Element to be removed from Second Array List is :" & arr_lst1.Item(i - 1))
arr_lst1.RemoveAt(i - 1)
End Sub
Fig80



Fig81



Fig82



Following is the complete Program for creating the array list, displaying the elements of array list created and for removing the elements from the created array list.

Public Class Status_from
Dim arr_lst, arr_lst1 As New ArrayList
Private Sub Array_list_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Array_list.Click
Dim str() As String = {"TCP/IP", "HTML", "PHP", "Android", "ASP.Net"}
'String Array With arr_lst 'Adding Elements to the Array List One By One Using With 'Statement
.Add("C Programming")
.Add("Visual Basic")
.Add("Data Mining")
.Add("Discrete Maths")
End With
arr_lst1.AddRange(str)
' We Can also Add the Range of Value by AddRange
MsgBox("Array Lists are Created")
End Sub

Private Sub btndisAr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisAr.Click
MsgBox(" Dispalying First Array List using For Loop")
Dim i, j
Dim s As String = ""
For i = 0 To arr_lst.Count - 1
s = s & i + 1 & ": " & arr_lst(i) & ControlChars.CrLf
Next
MsgBox("Elements " & ControlChars.CrLf & s)
MsgBox(" Dispalying Second Array List using For Each Loop")
i = 0
s = ""
For Each j In arr_lst1
i += 1
s = s & i + 1 & ": " & j & ControlChars.CrLf
Next
MsgBox("Elements " & ControlChars.CrLf & s)
End Sub

Private Sub btnRemAL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemAL.Click
Dim i As Integer = Integer.Parse(InputBox("Enter the Position to delete : "))
MsgBox("Deleted Element from First Array List is :" & arr_lst.Item(i - 1))
arr_lst.RemoveAt(i - 1)
MsgBox("Deleted Element from First Array List is :" & arr_lst1.Item(i - 1))
arr_lst1.RemoveAt(i - 1)
End Sub
End Class

<< Previous   Next>>


comments powered by Disqus
click here
click here
click here
Footer1