|
-
Jun 15th, 2009, 12:20 PM
#1
Thread Starter
Addicted Member
Changing Upperbound
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.
-
Jun 15th, 2009, 12:59 PM
#2
Re: Changing Upperbound
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.
-
Jun 15th, 2009, 01:11 PM
#3
Re: Changing Upperbound
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)
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jun 15th, 2009, 01:46 PM
#4
Thread Starter
Addicted Member
Re: Changing Upperbound
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
-
Jun 15th, 2009, 02:03 PM
#5
Re: Changing Upperbound
 Originally Posted by stanav
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).
[/code]
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).
Last edited by SJWhiteley; Jun 15th, 2009 at 02:24 PM.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jun 15th, 2009, 02:28 PM
#6
Re: Changing Upperbound
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.
My usual boring signature: Nothing
 
-
Jun 15th, 2009, 03:25 PM
#7
Re: Changing Upperbound
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?
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jun 15th, 2009, 04:30 PM
#8
Re: Changing Upperbound
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
My usual boring signature: Nothing
 
-
Jun 15th, 2009, 04:57 PM
#9
Re: Changing Upperbound
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:
 Originally Posted by SJWhiteley
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.
Then that would mean the Count + 1 = Capacity, which is not True. The easiest way to see this is:
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
Last edited by ForumAccount; Jun 15th, 2009 at 05:02 PM.
-
Jun 15th, 2009, 06:05 PM
#10
Re: Changing Upperbound
I thought my way of seeing it was easier than that, but I didn't output anything.
My usual boring signature: Nothing
 
-
Jun 16th, 2009, 01:52 AM
#11
Re: Changing Upperbound
 Originally Posted by ForumAccount
Shaggy is right, it doubles when the Count = Capacity. (Although it starts at 4 on the first added item).
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:
vb.net Code:
Dim myList As New List(Of Object)(10)
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.
-
Jun 16th, 2009, 06:49 AM
#12
Re: Changing Upperbound
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...
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|