473,763 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2819
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.Capac ity
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.Remov e).

When the ArrayList.Count reaches its ArrayList.Capac ity 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
"IntegerBuf fer" 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.c om> wrote in message
news:dc******** *************** **@posting.goog le.com...
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.c om> 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******** ******@TK2MSFTN GP14.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.Capac ity 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.Remov e).

When the ArrayList.Count reaches its ArrayList.Capac ity 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 "IntegerBuf fer" 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.c om> wrote in message
news:dc******** *************** **@posting.goog le.com...
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
14034
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 value of a function acts like 'by Val' returning the value only (except for objects) can you make it return a reference instead? cheers, Krackers
26
9632
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
5745
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). Class B is functionally an array of A objects (representing a cashflow). Class C is functionally an array of B objects (representing an organization of cashflows).
0
2494
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 ArrayList variable has gone out of scope (all int and string properties still exist). It must be where an ArrayList is treated like an Object, but there are shortcuts in place for strings and ints....
11
1489
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
3132
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 very unhappy with the idea and the compiler refuses to play ball with any of the attempts I have made to build such a structure. There is no problem when using a single string but a two dimensional array of strings appears to be a very different...
4
1523
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 who experience. (For some reason I have to implement Array not ArrayLists) Below are the simple following code: Dim Array() As String Dim intCounter As Integer
8
71612
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 someone can help me. Thanks in advance!
15
1667
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
9389
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10149
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9828
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8825
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.