Beruflich Dokumente
Kultur Dokumente
NET
by: juliet
by:
This simple tutorial teaches you how to work with an ArrayList class. Many people who
switches from the C/C++ world found that there is no dynamic memory allocation in
the arrays of C#. Actually there is a class in .NET which allows you to do the same
functionality as dynamic memory allocation but is much simpler to use - the ArrayList
class. With ArrayList, you no longer need to worry about freeing up memory after
allocation of memory and array bound overflow.
Here is the code to build an ArrayList, and retrieve content from it:
using System;
using System.Collections;
class TestArrayList {
public static void Main() {
ArrayList myList = new ArrayList();
int i;
myList.Add("Hello");
To use it, you must "using" the System.Collections namespace since the ArrayList class
is defined in it. The Add() method of the ArrayList accept an Object instance as its
argument, therefore, as you can see, you can put into it an integer or a string alike, or any
user-defined class.
In the second for loop, you would see I had used the Count property of the ArrayList
class to retrieve the number of element in it. It is very convenient since you do not have
to book keep the number of element in this array. Finally, remember when you print the
content of the ArrayList to the screen, you have to use the ToString() method of the
element, since retrieval of an myList[i] element would return an Object type.
Module Module1
Sub Main()
list.Add("One")
list.Add("Two")
list.Add("Three")
End Sub
End Module
Using parameter
Here, we describe how it is possible and often useful to receive an ArrayList type as a
parameter to a method Sub. This enables you to exploit structured programming with
the ArrayList type to a larger extent. The Example method here could be used with
any ArrayList instance, with any elements in its internal storage.
--- Program that uses ArrayList with method (VB.NET) ---
Module Module1
Sub Main()
list.Add(5)
list.Add(7)
Example(list)
End Sub
''' <summary>
''' </summary>
Console.WriteLine(num)
Next
End Sub
End Module
Using AddRange
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 method. The AddRange
method receives one argument, which is an ArrayList type that contains elements
you want to add to the end of the instance ArrayList you call the method on. In this
example, the two array lists are effectively concatenated.
--- Program that uses AddRange method (VB.NET) ---
Module Module1
Sub Main()
list1.Add(5)
list1.Add(7)
list2.Add(10)
list2.Add(13)
list1.AddRange(list2)
Console.WriteLine(num)
Next
End Sub
End Module
10
13
Module Module1
Sub Main()
' Add two elements to the ArrayList.
list.Add(9)
list.Add(10)
Console.WriteLine(list.Count)
list.Clear()
Console.WriteLine(list.Count)
End Sub
End Module
Module Module1
Sub Main()
list.Add("Dot")
list.Add("Net")
list.Add("Perls")
list.RemoveAt(1)
list.Insert(0, "Carrot")
list.RemoveRange(0, 2)
' Display.
Console.WriteLine(str)
Next
End Sub
End Module
Perls
Using TryCast
In the ArrayList type, elements are not stored with a type directly; instead they are
accessed through the object base type. To cast an object to the more derived type
you want to use, please use the TryCast operator. The syntax for this operator
receives two arguments: the element you want to cast from the ArrayList, and then
the type to which you want to cast. The TryCast operator will not throw exceptions,
as it uses the tester-doer pattern.
--- Program that uses ArrayList and TryCast (VB.NET) ---
Module Module1
Sub Main()
list.Add("man")
list.Add("woman")
list.Add("plant")
Dim i As Integer
For i = 0 To list.Count - 1
Console.WriteLine(str)
Next i
End Sub
End Module
man
woman
plant
Using GetRange
In this example, we look at how you can programmatically extract one part of an
ArrayList instance into another ArrayList instance. To do this, please use the
GetRange instance method on the original ArrayList instance. Then, assign the result
of the GetRange method call to a new ArrayList variable reference. The GetRange
uses the standard pattern for range-based methods: it receives the starting index
from which you want to copy, and then the count of elements you want to get.
--- Program that uses ArrayList and GetRange (VB.NET) ---
Module Module1
Sub Main()
list1.Add("fish")
list1.Add("amphibian")
list1.Add("bird")
list1.Add("plant")
' Create a new ArrayList and fill it with the range from the first
one.
list2 = list1.GetRange(2, 2)
Console.WriteLine(str)
Next
End Sub
End Module
bird
plant
Sub Page_Load
' Add the colors to the array
ColorList(0) = "Red"
ColorList(1) = "Orange"
ColorList(2) = "Yellow"
ColorList(3) = "Green"
ColorList(4) = "Blue"
ColorList(5) = "Indigo"
ColorList(6) = "Violet"
If Not Page.IsPostback
Dim ColorName as String
ddlFontList.DataSource = FontList
ddlFontList.DataBind()
End If
End Sub
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
Select a color from the list:<asp:DropDownList id="ddlColorList" runat="ser
</p>
<p>
Then select a font sytle from the list:
<asp:DropDownList id="ddlFontList" runat="server"></asp:DropDownList>
</p>
<p>
<asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat=
</p>
<p>
<asp:Label id="lblOutputMessage" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
VB.Net Tutorial
When you execute this program , at first add five items in the arraylist and
displays. Then again one more item inserted in the third position , and then sort
all items. Next it remove the item1 and also remove the item in the third
position . Finally it shows the existing items.
• Print this
• E-mail this
• Leave a comment
• Clip this
•
Close
The ability to use collections is important in any language and practical for any application. Collections allow you to manage groups of objects.
In this article, I'll look at how to use an ArrayList class, which provides basic functionality that is useful to most applications. I'll also cover the basics about the methods
within the class.
An ArrayList class
An ArrayList class represents a dynamically sized, index-based collection of objects. In the cases where you don't know the number of objects that you would store, an
ArrayList class is a good option to utilize.
The System.Collections.ArrayList class provides general collection functionality that is suitable for many uses. The class allows you to dynamically add and remove items
from a list. Items are accessed by using an index of the item.
This is the default property for collections in VB.NET. This means you can access it without using the Item property name, allowing syntax similar to that of an array.
As items are removed from a collection, the index numbers are reassigned to occupy any available spaces; so, index values are not static and might not always return the
same reference.
VB.Net Tutorial
When you execute this program add seven items in the stack .
Then its checks the item "Wed" exist in the Stack. If the item
exist in the Stack , it Pop the last item from Stack , else it
shows the msg "Not Exist"
HashTable stores a Key Value pair type collection of data . We can retrive
items from hashTable to provide the key . Both key and value are Objects.
The common functions using in Hashtable are :
Add : To add a pair of value in HashTable
Syntax : HashTable.Add(Key,Value)
Key : The Key value
Value : The value of corrosponding key
ContainsKey : Check if a specified key exist or not
Synatx : HashTable.ContainsKey(key)
Key : The Key value for search in HahTable
ContainsValue : Check the specified Value exist in HashTable
Synatx : HashTable.ContainsValue(Value)
Value : Search the specified Value in HashTable
Remove : Remove the specified Key and corrosponding Value
Syntax : HashTable.Remove(Key)
Key : The argument key of deleting pairs
The following source code shows all important operations in a HashTable
VB.Net Tutorial
VB.Net Tutorial
For i = 0 To week.Length - 1
MsgBox(week(i))
Next
End Sub
End Class
VB.Net Tutorial
When you execute the program it add seven items in the Queue. Then it
Dequeue (remove) the oldest item from queue. Next it Peek() the oldest item
from Queue (shows only , not remove ). Next it check the Item "Sun"
contains in the Queue.
VB.Net Tutorial
When you execute the program it add seven items in the Queue. Then it
Dequeue (remove) the oldest item from queue. Next it Peek() the oldest item
from Queue (shows only , not remove ). Next it check the Item "Sun"
contains in the Queue.
Dynamic Arrays can resize the capability of the Array at runtime .when you
are in a situation that you do not know exactly the number of elements to
store in array while you making the program. In that situations we are using
Dynamic Array .
Initial declaration
Dim scores() As Integer
Resizing
ReDim scores(1)
If you want to keep the existing items in the Array , you can use the keyword
Preserve .
ReDim Preserve scores(2)
In this case the Array dynamically allocate one more String value and keep
the existing values.
VB.Net Tutorial
ReDim scores(1)
scores(0) = 100
scores(1) = 200
For i = 0 To scores.Length - 1
MsgBox(scores(i))
Next
scores(2) = 300
For i = 0 To scores.Length - 1
MsgBox(scores(i))
Next
End Sub
End Class
VB.Net Tutorial
Imports System.Collections.Specialized
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class