Anda di halaman 1dari 10

Example #1:

==============================

Module Module1
Sub Main()
Dim obj As New ArrayList
obj.Add("One")
obj.Add("Two")
obj.Add("Three")
obj.Add("Four")
obj.Add("Five")
Console.WriteLine("Number of items in the ArrayList are : " & obj.Count)
Console.ReadLine()
End Sub
End Module

Output #1:
Number of items in the ArrayList are : 5

Example #2:
==============================

Module Module2
Sub Main()
Dim obj As New ArrayList
obj.Add("One")
obj.Add("Two")
obj.Add("Three")
obj.Add("Four")
obj.Add("Five")
Console.WriteLine("Number of items in the ArrayList are :" & obj.Count)
Dim i As Integer
For i = 0 To obj.Count - 1
Console.WriteLine(obj.Item(i))
Next
Console.ReadLine()
End Sub
End Module

Output #2:
Number of items in the ArrayList are : 5
One
Two
Three
Four
Five

Example #3:
==============================

Module Module3
Sub Main()
Dim obj As New ArrayList
obj.Add("One")
obj.Add("Two")
obj.Add("Three")
obj.Add("Four")
obj.Add("Five")
Console.WriteLine("Number of items in the ArrayList are :" & obj.Count)
Dim e As IEnumerator
e = obj.GetEnumerator
Dim s As String
Do While e.MoveNext
s = CType(e.Current, String)
Console.WriteLine(s)
Loop
e.Reset()

Dim p As String = "Six"


obj.Insert(2, p)
Console.WriteLine("After Inserting Number of items in the ArrayList are :" &
obj.Count)
Dim i As Integer
Console.WriteLine("Following Items are available")
For i = 0 To obj.Count - 1
Console.WriteLine(obj.Item(i))
Next
If obj.Contains(p) Then
Console.WriteLine("Record Found!")
Else
Console.WriteLine("Record Not Found!")
End If

Dim index As Integer


index = obj.IndexOf(p)
Console.WriteLine("The index of Six is " & index)

If obj.IsReadOnly Then
Console.WriteLine("Readonly")
Else
Console.WriteLine("Not Readonly")
End If

obj.RemoveAt(2)
Console.WriteLine("After Removing Number of items in the ArrayList are :" &
obj.Count)
Console.WriteLine("Following Items are available")
For i = 0 To obj.Count - 1
Console.WriteLine(obj.Item(i))
Next
If obj.Contains(p) Then
Console.WriteLine("Record Found!")
Else
Console.WriteLine("Record Not Found!")
End If
obj.Clear()
Console.WriteLine("Number of items in the ArrayList are :" & obj.Count)
Console.ReadLine()
End Sub
End Module
Output #3:
Number of items in the ArrayList are : 5
One
Two
Three
Four
Five
After Inserting Number of items in the ArrayList are : 6
One
Two
Six
Three
Four
Five
Record Found!
The index of Six is 2
Not Readonly
After Removing Number of items in the ArrayList are : 5
Following Items are available
One
Two
Three
Four
Five
Record Not Found!
Number of items in the ArrayList are : 0

Example #4:
==============================

Module Module4
Sub Main
Dim ht As New HashTable
ht.Add("1","One")
ht.Add("2","Two")
ht.Add("3","Three")
ht.Add("4","Four")
ht.Add("5","Five")
Console.WriteLine("Number of items in the HashTable are : " & ht.Count)
Console.ReadLine
End Sub
End Module

Output #4:
Number of items in the HashTable are : 5

Example #5:
==============================

Module Module5
Sub Main()
Dim ht As New Hashtable
Dim P1 As New Person(111, "Saravanan", 26)
Dim P2 As New Person(222, "Selvakumar", 26)
Dim P3 As New Person(333, "Venkatesh", 27)
ht.Add(P1.id, P1)
ht.Add(P2.id, P2)
ht.Add(P3.id, P3)
Console.WriteLine("Number of items in the HashTable are : " & ht.Count)
Dim P As Person
If ht.Contains(111) Then
Console.WriteLine("Record Found!")
P = CType(ht.Item(111), Person)
Console.WriteLine("The age of " & P.name & " is " & P.age)
Else
Console.WriteLine("Record Not Found!")
End If
Console.ReadLine()
End Sub
End Module

Public Class Person


Public id As Integer
Public name As String
Public age As Integer

Public Sub New(ByVal i As Integer, ByVal s As String, ByVal a As Integer)


id = i
name = s
age = a
End Sub
End Class

Output #5:
Number of items in the HashTable are : 3
Record Found
The age of Saravanan is 26

Example #6:
==============================

Module Module6
Sub Main
Dim st As New SortedList
st.Add("1","One")
st.Add("2","Two")
st.Add("3","Three")
st.Add("4","Four")
st.Add("5","Five")
Console.WriteLine("Number of items in the SortedList are :" & st.Count)
Console.ReadLine
End Sub
End Module
Output #6:
Number of items in the SortedList are : 5

Example #7:
==============================

Module Module7
Sub Main()
Dim st As New SortedList
Dim P1 As New Person(111, "Saravanan", 26)
Dim P2 As New Person(222, "Selvakumar", 26)
Dim P3 As New Person(333, "Venkatesh", 27)
st.Add(P1.id, P1)
st.Add(P2.id, P2)
st.Add(P3.id, P3)
Console.WriteLine("Number of items in the SortedList are : " & st.Count)
Dim P As Person
If st.Contains(111) Then
Console.WriteLine("Record Found!")
P = CType(st.Item(111), Person)
Console.WriteLine("The age of " & P.name & " is " & P.age)
Else
Console.WriteLine("Record Not Found!")
End If
Console.ReadLine()
End Sub
End Module

Public Class Person


Public id As Integer
Public name As String
Public age As Integer

Public Sub New(ByVal i As Integer, ByVal s As String, ByVal a As Integer)


id = i
name = s
age = a
End Sub
End Class

Output #7:
Number of items in the SortedList are : 3
Record Found!
The age of Saravanan is 26

Example #8:
==============================

Module Module8
'Using GetByIndex
Sub Main()
Dim st As New SortedList
Dim P1 As New Person(111, "Saravanan", 26)
Dim P2 As New Person(222, "Selvakumar", 26)
Dim P3 As New Person(333, "Venkatesh", 27)
st.Add(P1.id, P1)
st.Add(P2.id, P2)
st.Add(P3.id, P3)
Console.WriteLine("Number of items in the SortedList are : " & st.Count)
Dim P As Person
Dim i As Integer
For i = 0 To st.Count - 1
P = CType(st.GetByIndex(i), Person)
Console.WriteLine("The age of " & P.name & " is " & P.age)
Next
Console.ReadLine()
End Sub
End Module

Public Class Person


Public id As Integer
Public name As String
Public age As Integer

Public Sub New(ByVal i As Integer, ByVal s As String, ByVal a As Integer)


id = i
name = s
age = a
End Sub
End Class

Output #8:
Number of items in the SortedList are : 3
The age of Saravanan is 26
The age of Selvakumar is 26
The age of Venkatesh is 27

Example #9:
==============================

Module Module9
'Using Enumerator and DictionaryEntry
Sub Main()
Dim st As New SortedList
Dim P1 As New Person(111, "Saravanan", 26)
Dim P2 As New Person(222, "Selvakumar", 26)
Dim P3 As New Person(333, "Venkatesh", 27)
st.Add(P1.id, P1)
st.Add(P2.id, P2)
st.Add(P3.id, P3)
Console.WriteLine("Number of items in the SortedList are :" & st.Count)
Dim P As Person
Dim i As Integer
i = 0
Dim e As IEnumerator, de As DictionaryEntry
e = st.GetEnumerator
Do While e.MoveNext
de = CType(e.Current, DictionaryEntry)
P = CType(de.Value, Person)
Console.WriteLine("The age of " & P.name & " is " & P.age)
i = i + 1
Loop
Console.ReadLine()
End Sub
End Module

Public Class Person


Public id As Integer
Public name As String
Public age As Integer

Public Sub New(ByVal i As Integer, ByVal s As String, ByVal a As Integer)


id = i
name = s
age = a
End Sub
End Class

Output #9:
Number of items in the SortedList are : 3
The age of Saravanan is 26
The age of Selvakumar is 26
The age of Venkatesh is 27

Example #10:
==============================

Module Module10
Sub Main()
Dim q As New Queue
q.Enqueue("One")
q.Enqueue("Two")
q.Enqueue("Three")
q.Enqueue("Four")
q.Enqueue("Five")
Console.WriteLine("Number of items in the Queue are : " & q.Count)

While q.Count > 0


Console.WriteLine(q.Dequeue())
End While
Console.ReadLine()
End Sub
End Module

Output #10:
Number of items in the Queue are : 5
One
Two
Three
Four
Five

Example #11:
==============================
Module Module11
Sub Main
Dim s As New Stack
s.Push("One")
s.Push("Two")
s.Push("Three")
s.Push("Four")
s.Push("Five")
Console.WriteLine("Number of items in the Stack are : " & s.Count)

While s.Count > 0


Console.WriteLine(s.Pop())
End While
Console.ReadLine
End Sub
End Module

Output #11:
Number of items in the Stack are : 5
One
Two
Three
Four
Five
===============================================================================
====================
FAQ
===============================================================================
====================
How to add items dynamically to a ListBox using an arraylist? (For Aspx)
In aspx
< asp: ListBox id="ListBox1" runat="server">
In aspx.vb
Dim arrList As New ArrayList
arrList.Add("One")
arrList.Add("Two")
arrList.Add("Three")
arrList.Add("Four")
arrList.Add("Five")
ListBox1.DataSource = arrList
ListBox1.DataBind ()
===============================================================================
====================
How to bind an Arraylist to a Dropdown List? (For Aspx)
Dim arrLst As ArrayList = New ArrayList
arrLst.Add("One")
arrLst.Add("Two")
arrLst.Add("Three")
arrLst.Add("Four")
arrLst.Add("Five")
DropDownList1.DataSource = arrLst
DropDownList1.DataBind ()
===============================================================================
====================
Which of the following class does not belong to Collection namespace?
i) ArrayList
ii) Queue
iii)Stack
iv) DictionaryList (Ans)
===============================================================================
====================
Which is not a collection type in VB.Net?
i) Hashtable
ii) ArrayList
iii)Heap (Ans)
iv) Stack
===============================================================================
====================
Which collection object sorts the item implicitly?
i) ArrayList
ii) Hashtable
iii)Stack
iv) SortedList (Ans)
v) Queue
===============================================================================
====================
How to identify memory spaces in ArrayList?
i) FindMemory
ii) MemorySpace
iii)Capacity (Ans)
iv) ArrayMemory
v) Value
===============================================================================
====================
What's the .Net data type that allows the retrieval of data by a unique key?
HashTable
===============================================================================
====================
A 'Stack' Collection provides 'first-in-first-out' functionality:
i) True
ii)False (Ans)
Explanation: A stack provides last-in-first-out functionality
===============================================================================
====================
A 'Queue' Collection provides 'first-in-first-out' functionality:
i) True (Ans)
ii)False
===============================================================================
====================
The IList interface is implemented by indexed collections
i) True (Ans)
ii)False
===============================================================================
====================
The IDictionary interface is implemented by sorted collections
i) True
ii)False (Ans)
===============================================================================
====================
All collection classes implement ICollection
i) True
ii)False (Ans)
Explanation: ArrayList implements IList, ICollection, IClonable
HashTable implements ICollection, IDicitionary
SortedList implements ICollection, IDictionary
===============================================================================
====================
The HashTable is faster on searches than SortedList
i) True (Ans)
ii)False
===============================================================================
====================
The ArrayList is using less memory than a SortedList
i) True (Ans)
ii)False
===============================================================================
====================
A SortedList collection is
i) A Collection of values sorted using a specified key.
ii) A Collection of key-value pairs (Ans)
iii)A Collection that implements IEnumerable
iv) A Collection that implements IList
===============================================================================
====================
Summary
=========

Anda mungkin juga menyukai