Is it possible to change the upperbound of an array once it has been instantiated? I want to change the upperbound in an array so it matches the number of items that hold a value.
Cheers.
Printable View
Is it possible to change the upperbound of an array once it has been instantiated? I want to change the upperbound in an array so it matches the number of items that hold a value.
Cheers.
Just create an array based on the number of items holding a value....I don't think its possible to actually change the array like you want.
While you can change the size of an array by using ReDim or ReDim Preserve keywords, resizing an array is generally costly (in term of CPU cycles). The better approach is to use some kind of collection based type, such as List(Of T).
Anyway, here is how you change the size of an array (thus changing its upperbound)
Code:'Declare an array with 1 element
Dim arr(0) As Integer
'Change that array to 5 elements discarding the existing values
ReDim arr(4)
'Change that array again to 10 elements keeping the existing values
ReDim Preserve arr(9)
Redim works great. Had to assign it to a new array first using dim though as it wouldn't work on a private decalred array :thumb:
You'd better check and see what a list does when you add items to it if you haven't declared a size - It redimensions an array... ;) Even so, using a list is still a good idea for collections of objects which one is going to manipulate.
(Actually, that's not strictly true. If there isn't enough room in the array, then it creates a new array of the required size and copies the existing values to that new array - probably what Redim Preserve actually does).
That's not quite it, either. A list starts out with some size. When you add an item beyond that size, it doubles the size. Each time it resizes, it doubles. This reduces the frequency of resizing.
You can also add arrays into Lists quite easily, and turn Lists back into arrays. Therefore, there is no good reason to use an array that doesn't have a fixed size.
Note that Redim will erase the array, while Redim Preserve will not. Also note that Redim will only work on the last dimension of an array, so you can't redimension multidimensional arrays without extensive copying.
Not sure what you meant by needing to copy it, something wasn't quite right in that statement, as there is no issue with redimensioning private arrays. I did it extensively before moving to 2005 where the List (of T) was introduced.
No, a list doesn't double its capacity. When you add an item to it, it only ensures there's is enough capacity. If there isn't it increases the size by one. So, each time you add an item to a list it'll recreate the internal array. The default capacity is zero for the List object.
But lets make it clear, we are talking about the System.Collections.Generic.List(of T) object, aren't we?
Yes it does double capacity. There may be an upper limit, but test this out to see:
Code:Dim mL As New List(Of String)
Dim x As Integer
For x = 0 To 33
mL.Add(mL.Capacity.ToString)
Next
Shaggy is right, it doubles when the Count = Capacity. (Although it starts at 4 on the first added item). SJWhiteley, if it were like this:
Then that would mean the Count + 1 = Capacity, which is not True. The easiest way to see this is:Quote:
Originally Posted by SJWhiteley
vb.net Code:
Dim myList As New List(Of String) Dim myCaps As New List(Of Integer)(New Integer() {0}) For x As Integer = 0 To 10000 myList.Add(x.ToString()) If Not myCaps.Contains(myList.Capacity) Then myCaps.Add(myList.Capacity) End If Next For Each i As Integer In myCaps Debug.WriteLine(i.ToString()) Next
I thought my way of seeing it was easier than that, but I didn't output anything.
As another aside, it's worth noting that 4 is just the default initial size and you shouldn't accept that if you know for a fact that it will end up bigger. For instance, if you know for a fact that your List will end up containing at least 10 items then you should should specify that capacity when you create it:That way the internal array won't need to be resized until the 11th item is added, at which point the capacity is doubled to 20. If the count never passes 10 then no resizing is needed at all.vb.net Code:
Dim myList As New List(Of Object)(10)
Yeah, you are right. The initial capacity is zero, the first item added it goes to 4, then goes to the current size * 2. I had tested by adding 8 items...