473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Array vs arraylist variable scope

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
4 2794
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
26
by: JGH | last post by:
How can I check if a key is defined in an associative array? var users = new array(); users = "Joe Blow"; users = "John Doe"; users = "Jane Doe"; function isUser (userID) { if (?????)
1
by: Craig | last post by:
Hey Everyone - I'm trying to determine the fastest way of moving array information around and could use some help. Here's the setup: Class A stores a DateTime and an amount (a payment)....
0
by: Pierson C | last post by:
A quick easy one! I have a custom user control that has an ArrayList property. I create an instance of the user control, assign all of the properties, but when I add the control to the page, my...
11
by: Just Me | last post by:
This doesn't work. I guess New is needed someplace but I don't know where? Public Property Filters() As ArrayList Get Filters.Add(cboFilter.Text)
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
4
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those...
8
by: Pim75 | last post by:
Hello, I'm defining a string array like: Dim strArray() As String = {"1", "2"} Can I add some values to this string array later in the code? It's not clear to me how to do this. I hope...
15
by: mdh | last post by:
May I ask. If an array is defined , not as a static, but outside of a function, is there any guarantee as to the contents of each element? Thanks.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.