Array vs Arraylist

torg

Regular
Joined
Nov 7, 2002
Messages
60
Location
Norway
I`ve just began to explore the use of Arraylist. It seems to me that there ar only advantages by using Arraylists instead of Arrays? I would like to hear what others think about this. I also wonder if there are "situations" where ordinary arrays are preferable instead of Arraylist.

Could anyone tell me how to do a binary search in my example down below? Let`s say I want to search for "!".

Visual Basic:
Dim myAL As New ArrayList()
        myAL.Add("Hello")
        myAL.Add("World")
        myAL.Add("!")
        Dim i As Integer
        For i = 0 To 2
            lstData.Items.Add(myAL.Item(i))
 
ArrayList = best used for dynamic list.
Array = best used for static list.

If the size of your list can change, use an ArrayList. If it doesn't change, use an array.
 
Using your example from above the following should work
Visual Basic:
Dim myAL As New ArrayList
myAL.Add("Hello")
myAL.Add("World")
myAL.Add("!")

Dim i As Integer

myAL.Sort()
i = myAL.BinarySearch("!")
 
Back
Top