分享

ArrayList详细使用方法的实例

 nxhujiee 2010-04-15
ArrayList详细使用方法的实例
作者:佚名    来源:乐博网收集     更新时间:2007-11-28

Adding Objects to the ArrayList

Adding objects is simple just use the add method:

Dim al As New ArrayList()
            al.Add("This")
            al.Add("Is")
            al.Add("My")
            al.Add("New")
            al.Add("ArrayList")
            al.Add("Control")
            

This adds 6 new objects to the arraylist.

 

Reading the Values of the ArrayList

To read the values you can iterate though each of the items in the Item collection like so:

Dim i As Integer
            For i = 0 To al.Count - 1
            Console.WriteLine(CStr(al.Item(i)))
            Next
            

If you prefer you can use an Enumerator like this:

Dim en As IEnumerator = al.GetEnumerator
            While en.MoveNext
            Console.WriteLine(en.Current)
            End While
            

 

Searching The ArrayList

To search for the existence of an object in the ArrayList, you can use the BinarySearch method:

Dim al As New ArrayList()
            al.Add("This")
            al.Add("Is")
            al.Add("My")
            al.Add("New")
            al.Add("ArrayList")
            al.Add("Control")
            'Do a case Insensitive search
            Dim idx As Integer = al.BinarySearch("my", New CaseInsensitiveComparer())
            If idx > 0 Then
            MessageBox.Show("Found at Index " & idx)
            Else
            MessageBox.Show("Not Found")
            End If
            

Note that we use a CaseInsensitiveComparer object above, you could also do this:

Dim idx As Integer = al.BinarySearch("my")
            

If you do not specify the IComparer implementation the default is a case sensitive compare. So the line above will return the "Not Found" message.

 

Sorting the ArrayList Ascending

There are a few different options to sorting but the basic is a simple Ascending Case Sensitive Sort:

This code assumes there is a button on your form called Button1

Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles Button1.Click
            Dim al As New ArrayList()
            al.Add("This")
            al.Add("Is")
            al.Add("My")
            al.Add("New")
            al.Add("ArrayList")
            al.Add("Control")
            Console.WriteLine("Before Sort:")
            WriteElements(al)
            al.Sort()
            Console.WriteLine("After Sort:")
            WriteElements(al)
            End Sub
            Private Sub WriteElements(ByVal al As ArrayList)
            Dim i As Integer
            For i = 0 To al.Count - 1
            Console.WriteLine(CStr(al.Item(i)))
            Next
            End Sub
            

Here is what the output will look like in the console window:

Before Sort:
            This
            Is
            My
            New
            ArrayList
            Control
            After Sort:
            ArrayList
            Control
            Is
            My
            New
            This
            

The sort command physically reorders each object, so you can even use the Enumerator to list each of the values and you will get the same value, shown in the next example below.

 

Descending ArrayList Sort

This can be done by adding one line to the code above:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim al As New ArrayList()
            al.Add("This")
            al.Add("Is")
            al.Add("My")
            al.Add("New")
            al.Add("ArrayList")
            al.Add("Control")
            Console.WriteLine("Before Sort:")
            WriteElements(al)
            al.Sort()
            al.Reverse()
            Console.WriteLine("After Sort:")
            WriteElements(al)
            End Sub
            Private Sub WriteElements(ByVal al As ArrayList)
            Dim en As IEnumerator = al.GetEnumerator
            While en.MoveNext
            Console.WriteLine(en.Current)
            End While
            End Sub
            

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多