473,395 Members | 1,458 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,395 software developers and data experts.

What is the best way to expand an array?

Is this sequence about as efficient as I can get for sometimes expanding an
array?

MyClass [] MyFunc()
{
MyClass [] result = new MyClass [0];

{
// Do stuff that might possibly allocate an array and put entries in
it
// This path is usually executed
// ...
result = new MyClass[numEntries];
}

{
// Do stuff that might possibly add more entries to the previously
allocated array
// This path may be executed, but not usually
// ...
newResult = new MyClass[result .Length + newNumEntries];
result .CopyTo(newResult , 0);
result = newResult ;
}

return result;
}
Nov 15 '05 #1
4 10266
Unless performance is a major issue, it would probably be easier just to use
an ArrayList

MyClass[] MyFunc()
{
ArrayList result = new ArrayList();
{
//do stuff to fill the result
}
return (MyClass[])result.ToArray(typeof(MyClass));
}
"David Rogers" <dr*****@NOSPAM.fhcrc.org> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Is this sequence about as efficient as I can get for sometimes expanding an array?

MyClass [] MyFunc()
{
MyClass [] result = new MyClass [0];

{
// Do stuff that might possibly allocate an array and put entries in it
// This path is usually executed
// ...
result = new MyClass[numEntries];
}

{
// Do stuff that might possibly add more entries to the previously
allocated array
// This path may be executed, but not usually
// ...
newResult = new MyClass[result .Length + newNumEntries];
result .CopyTo(newResult , 0);
result = newResult ;
}

return result;
}

Nov 15 '05 #2
David Rogers <dr*****@NOSPAM.fhcrc.org> wrote:
Is this sequence about as efficient as I can get for sometimes expanding an
array?


Yes - but most of the time when you want a dynamically-sized list of
things, you might as well use ArrayList instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
David,
Is this sequence about as efficient as I can get for sometimes expanding an array? For the most part yes.

You could do as the ArrayList & StringBuilder classes do.

Over allocate the array, and keep track of how many items are actually in
the array. When the array gets full, then you double it in size. Of course
that may be what you are already doing ;-)

There was a discussion about creating an new ArrayList like class for
structures in the vb.dotnet.technical newsgroup (on the news.devx.com news
server). Look for a thread titled 'Dynamic array of structure variables,
starting 2 Aug 2003.

http://news.devx.com/cgi-bin/dnewswe...tnet.technical

I gave a start of a class to create a dynamic array of structures, that over
allocates the underlying array.

Hope this helps
Jay

"David Rogers" <dr*****@NOSPAM.fhcrc.org> wrote in message
news:%2***************@tk2msftngp13.phx.gbl... Is this sequence about as efficient as I can get for sometimes expanding an array?

MyClass [] MyFunc()
{
MyClass [] result = new MyClass [0];

{
// Do stuff that might possibly allocate an array and put entries in it
// This path is usually executed
// ...
result = new MyClass[numEntries];
}

{
// Do stuff that might possibly add more entries to the previously
allocated array
// This path may be executed, but not usually
// ...
newResult = new MyClass[result .Length + newNumEntries];
result .CopyTo(newResult , 0);
result = newResult ;
}

return result;
}

Nov 15 '05 #4
Hi David,

Use an ArrayList instead, it grows and shrinks automatically. The ArrayList
is not strongly typed but once you've finished populating it you can use the
CopyTo method to copy the data into a strongly typed array.

--
Rob Windsor
G6 Consulting
Toronto, Canada
"David Rogers" <dr*****@NOSPAM.fhcrc.org> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Is this sequence about as efficient as I can get for sometimes expanding an array?

MyClass [] MyFunc()
{
MyClass [] result = new MyClass [0];

{
// Do stuff that might possibly allocate an array and put entries in it
// This path is usually executed
// ...
result = new MyClass[numEntries];
}

{
// Do stuff that might possibly add more entries to the previously
allocated array
// This path may be executed, but not usually
// ...
newResult = new MyClass[result .Length + newNumEntries];
result .CopyTo(newResult , 0);
result = newResult ;
}

return result;
}

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
2
by: Randy Starkey | last post by:
Hi, Can anyone point me in the right direction on this? Is there a way to expand and collapse all instances of this script on a single page? The script works well individually. I do the HTML...
8
by: sathyashrayan | last post by:
/* ** PLURALTX.C - How to print proper plurals ** public domain - original algorithm by Bob Stout */ #include <stdio.h> #define plural_text(n) &"s"
41
by: Mountain Bikn' Guy | last post by:
What is the current preferred way to save user preferences in dotnet? Is the registry the right place to do this? Can anyone recommend a good article (or book) for this topic? Thanks.
7
by: Benzi Eilon | last post by:
I am executing an Expand command in order to expand a CAB file. This is done by calling CreateProcess with the Expand command and then WaitForSingleObject and checking the process exit code. My C++...
6
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for...
11
by: Nospam | last post by:
I don't know what it is I am doing wrong, I am trying to get the menus to either expand or contract based on their previous states, i.e if already expanded if clicked again contract, and if...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.