By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,885 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,885 IT Pros & Developers. It's quick & easy.

Array vs arraylist variable scope

P: n/a
I run into this situation all the time and I'm wondering what is the
most efficient way to handle this issue:

I'll be pulling data out of a data source and want to load the data
into an array so that I can preform complicated operations against
this data. The returned record count in these operations is always
variable.

1. I have been using an arraylist.add function to handle
non-multidemional returns but was wondering if I'm better off diming
these returns as a strString() and then redimin with presearve as the
values come in?

Example

Dim StrString() as string

dim i as integer = 0

for data looping....
redim presearve StrString(i + 1)
i +=1
next

the next question would be in the multidemin.

2. If you have a sql statement that returns multi demensional
information what is the best way to normally handle these situations.
When you do not know the record count.

Example:

sql = select Value1, Value2Subset, Value3Subset
Dim sqlHold(1, 3)

If this was more than 1000 rows.... what type of variable scope should
I use.
sqlHold(0,0) = Value1
sqlHold(0,1) = Value2Subset
sqlHold(0,1) = Value3Subset
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Peter,
Profile (time) it and see.

You will find that ArrayList is almost always more efficient. This is
because the ArrayList over allocates its internal array. ArrayList.Capacity
represents the number of elements the array list can hold (the size of its
internal array), while ArrayList.Count represents the number of elements
currently in the ArrayList (the number of times you called ArrayList.Add
without a ArrayList.Remove).

When the ArrayList.Count reaches its ArrayList.Capacity the ArrayList will
reallocate its internal array, effectively it does a "Redim Preserve", it
does this by allocating a new internal array, copying the contents of the
old internal array to the new internal array. It doubles the size of its
internal array each time it needs to reallocate it. The Capacity defaults to
16 if you do not change it via the Constructor when you create the
ArrayList. NOTE: If I plan on adding 1000 elements to the ArrayList I would
create the ArrayList with a Capacity of 1000 via the constructor.

When you use "Redim Preserve" internally VB creates a new array, then copies
all the elements from the old array to the new array. If you are Redim
Preserve with a factor of 1, then you are doing a lot of coping, especially
if you have 1000 elements!

Remember that an ArrayList's internal array is an array of Object, so if you
are storing Integers, they will be boxed going in & coming out. This may or
may not be a performance issue. You need to profile it and see. If the
boxing is causing a performance issue (proven via profiling), then what I do
is create an IntegerBuffer. Which is like an ArrayList & StringBuilder, it
maintains an over allocated internal array of integers, with Capacity &
Count properties, when I add integers to the IntegerBuffer, I increment the
Count, when the Count reaches Capacity I "Redim Preserve" the internal
array, doubling its size.

Just Remember the 80/20 rule. That is 80% of the execution time of your
program is spent in 20% of your code. I will optimize (worry about
performance, memory consumption) the 20% once that 20% has been identified &
proven to be a performance problem via profiling (CLR Profiler is one
profiling tool).

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware...timization.pdf

Info on the CLR Profiler:
http://msdn.microsoft.com/library/de...nethowto13.asp

http://msdn.microsoft.com/library/de...anagedapps.asp
NOTE: In VS.NET 2005 (aka Whidbey, due out later in 2005)
http://lab.msdn.microsoft.com/vs2005/ we will have Generics, which will give
us a generic "ArrayList" class called List(Of T) which can then use as our
"IntegerBuffer" above. http://msdn2.microsoft.com/library/6sh2ey19.aspx
Rather then return a 2 dimensional array why not return a DataSet/DataTable?
Or a collection of Domain objects?

Hope this helps
Jay

"Peter" <pe***@mclinn.com> wrote in message
news:dc*************************@posting.google.co m...
I run into this situation all the time and I'm wondering what is the
most efficient way to handle this issue:

I'll be pulling data out of a data source and want to load the data
into an array so that I can preform complicated operations against
this data. The returned record count in these operations is always
variable.

1. I have been using an arraylist.add function to handle
non-multidemional returns but was wondering if I'm better off diming
these returns as a strString() and then redimin with presearve as the
values come in?

Example

Dim StrString() as string

dim i as integer = 0

for data looping....
redim presearve StrString(i + 1)
i +=1
next

the next question would be in the multidemin.

2. If you have a sql statement that returns multi demensional
information what is the best way to normally handle these situations.
When you do not know the record count.

Example:

sql = select Value1, Value2Subset, Value3Subset
Dim sqlHold(1, 3)

If this was more than 1000 rows.... what type of variable scope should
I use.
sqlHold(0,0) = Value1
sqlHold(0,1) = Value2Subset
sqlHold(0,1) = Value3Subset

Nov 21 '05 #2

P: n/a

"Peter" <pe***@mclinn.com> wrote
I run into this situation all the time and I'm wondering what is the
most efficient way to handle this issue:

I'll be pulling data out of a data source and want to load the data
into an array so that I can preform complicated operations against
this data. The returned record count in these operations is always
variable.
Which is why the ArrayList would work well....

1. I have been using an arraylist.add function to handle
non-multidemional returns but was wondering if I'm better off diming
these returns as a strString() and then redimin with presearve as the
values come in?
Stay with the ArrayList, that is what it was designed for.
the next question would be in the multidemin.

2. If you have a sql statement that returns multi demensional
information what is the best way to normally handle these situations. When you do not know the record count.


Use a Structure to hold the data, and another ArrayList to hold the
structures.

A strongly typed collection would probably be more appropreate, but
the ArrayList is ready for use out of the box....

LFS

Nov 21 '05 #3

P: n/a
Peter,

It is very simple, when you have a static array from which you know the rows
will never change. Than a fixed array. When not, than any array or
collection that implements IList or ICollection where my favorite when it is
simple the arraylist.

http://msdn.microsoft.com/library/de...ClassTopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps?

Cor


Nov 21 '05 #4

P: n/a
Doh!
You will find that ArrayList is almost always more efficient. I really should have said "performs faster", as the boxing of value types
may make it more inefficient...

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uU**************@TK2MSFTNGP14.phx.gbl... Peter,
Profile (time) it and see.

You will find that ArrayList is almost always more efficient. This is
because the ArrayList over allocates its internal array.
ArrayList.Capacity represents the number of elements the array list can
hold (the size of its internal array), while ArrayList.Count represents
the number of elements currently in the ArrayList (the number of times you
called ArrayList.Add without a ArrayList.Remove).

When the ArrayList.Count reaches its ArrayList.Capacity the ArrayList will
reallocate its internal array, effectively it does a "Redim Preserve", it
does this by allocating a new internal array, copying the contents of the
old internal array to the new internal array. It doubles the size of its
internal array each time it needs to reallocate it. The Capacity defaults
to 16 if you do not change it via the Constructor when you create the
ArrayList. NOTE: If I plan on adding 1000 elements to the ArrayList I
would create the ArrayList with a Capacity of 1000 via the constructor.

When you use "Redim Preserve" internally VB creates a new array, then
copies all the elements from the old array to the new array. If you are
Redim Preserve with a factor of 1, then you are doing a lot of coping,
especially if you have 1000 elements!

Remember that an ArrayList's internal array is an array of Object, so if
you are storing Integers, they will be boxed going in & coming out. This
may or may not be a performance issue. You need to profile it and see. If
the boxing is causing a performance issue (proven via profiling), then
what I do is create an IntegerBuffer. Which is like an ArrayList &
StringBuilder, it maintains an over allocated internal array of integers,
with Capacity & Count properties, when I add integers to the
IntegerBuffer, I increment the Count, when the Count reaches Capacity I
"Redim Preserve" the internal array, doubling its size.

Just Remember the 80/20 rule. That is 80% of the execution time of your
program is spent in 20% of your code. I will optimize (worry about
performance, memory consumption) the 20% once that 20% has been identified
&
proven to be a performance problem via profiling (CLR Profiler is one
profiling tool).

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware...timization.pdf

Info on the CLR Profiler:
http://msdn.microsoft.com/library/de...nethowto13.asp

http://msdn.microsoft.com/library/de...anagedapps.asp
NOTE: In VS.NET 2005 (aka Whidbey, due out later in 2005)
http://lab.msdn.microsoft.com/vs2005/ we will have Generics, which will
give us a generic "ArrayList" class called List(Of T) which can then use
as our "IntegerBuffer" above.
http://msdn2.microsoft.com/library/6sh2ey19.aspx
Rather then return a 2 dimensional array why not return a
DataSet/DataTable? Or a collection of Domain objects?

Hope this helps
Jay

"Peter" <pe***@mclinn.com> wrote in message
news:dc*************************@posting.google.co m...
I run into this situation all the time and I'm wondering what is the
most efficient way to handle this issue:

I'll be pulling data out of a data source and want to load the data
into an array so that I can preform complicated operations against
this data. The returned record count in these operations is always
variable.

1. I have been using an arraylist.add function to handle
non-multidemional returns but was wondering if I'm better off diming
these returns as a strString() and then redimin with presearve as the
values come in?

Example

Dim StrString() as string

dim i as integer = 0

for data looping....
redim presearve StrString(i + 1)
i +=1
next

the next question would be in the multidemin.

2. If you have a sql statement that returns multi demensional
information what is the best way to normally handle these situations.
When you do not know the record count.

Example:

sql = select Value1, Value2Subset, Value3Subset
Dim sqlHold(1, 3)

If this was more than 1000 rows.... what type of variable scope should
I use.
sqlHold(0,0) = Value1
sqlHold(0,1) = Value2Subset
sqlHold(0,1) = Value3Subset


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.