473,327 Members | 2,069 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,327 software developers and data experts.

Redimensioning an Array

I found an article that discussed dynamic allocation of an array but it seems
extremely overloaded in time requirements for function. The process is fine
if there's no data in it, but if there is an additional set of steps is
necessary that make it seem less than efficient:

tmp = Array.CreateInstance(...)
orig.CopyTo(tmp,0)
orig = tmp
I would have thought an Add/Remove method would have been provided and
solution would have been more efficient.

Help? Thoughts?
--
Brad Eck
<a href="http://www.sitesdynamic.com">SitesDynamic</a>
<a href="http://www.basketsetcetera.com">Baskets Etcetera</a>
Nov 17 '05 #1
4 2462
In the VB.NET world, there is the ReDim Preserve operation that lets you
dynamically resize an array while preserving it’s contents. Unfortunately,
here in the C# world, we do not have that option and have to use a method not
unlike what you found, that of creating a new array of the size we want,
copying the values from the old array to the new, and updating the reference
to point to the new array.

This is at least the case if you are using an actual array ([] style).
Another option you have is using the ArrayList class from System.Collections
which does incorporate the Add() and Remove() methods you are looking for,
however it internally pretty much does what was described above.

Brendan
"Brad" wrote:
I found an article that discussed dynamic allocation of an array but it seems
extremely overloaded in time requirements for function. The process is fine
if there's no data in it, but if there is an additional set of steps is
necessary that make it seem less than efficient:

tmp = Array.CreateInstance(...)
orig.CopyTo(tmp,0)
orig = tmp
I would have thought an Add/Remove method would have been provided and
solution would have been more efficient.

Help? Thoughts?
--
Brad Eck
<a href="http://www.sitesdynamic.com">SitesDynamic</a>
<a href="http://www.basketsetcetera.com">Baskets Etcetera</a>

Nov 17 '05 #2
Brad,

You can use the ArrayList type in the System.Collections namespace to
create a dynamically allocated array (it's really a wrapper to provide that
kind of functionality, arrays lengths are immutable once created).

In .NET 2.0, you can use the generic List<T> class which will do the
same thing but in a type-safe manner (which is also quicker when using value
types as well, because there is no boxing overhead that you get with the
ArrayList class).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brad" <br******@sitesdynamic.com> wrote in message
news:EC**********************************@microsof t.com...
I found an article that discussed dynamic allocation of an array but it
seems
extremely overloaded in time requirements for function. The process is
fine
if there's no data in it, but if there is an additional set of steps is
necessary that make it seem less than efficient:

tmp = Array.CreateInstance(...)
orig.CopyTo(tmp,0)
orig = tmp
I would have thought an Add/Remove method would have been provided and
solution would have been more efficient.

Help? Thoughts?
--
Brad Eck
<a href="http://www.sitesdynamic.com">SitesDynamic</a>
<a href="http://www.basketsetcetera.com">Baskets Etcetera</a>

Nov 17 '05 #3
Hi Brad ! :O)

When it comes to dynamic arrays, I usually go with the ArrayList class. The
ToArray() method it provides is really handy when you need to convert its
content back to a normal strongly-typed array.

--
Best Regards
Yanick

"Brad" <br******@sitesdynamic.com> a écrit dans le message de
news:EC**********************************@microsof t.com...
I found an article that discussed dynamic allocation of an array but it seems extremely overloaded in time requirements for function. The process is fine if there's no data in it, but if there is an additional set of steps is
necessary that make it seem less than efficient:

tmp = Array.CreateInstance(...)
orig.CopyTo(tmp,0)
orig = tmp
I would have thought an Add/Remove method would have been provided and
solution would have been more efficient.

Help? Thoughts?
--
Brad Eck
<a href="http://www.sitesdynamic.com">SitesDynamic</a>
<a href="http://www.basketsetcetera.com">Baskets Etcetera</a>

Nov 17 '05 #4
Brendan Grant <gr****@NOSPAMdahat.com> wrote:
In the VB.NET world, there is the ReDim Preserve operation that lets you
dynamically resize an array while preserving it?s contents.


It doesn't really. It creates a new array of a different size and
copies the contents of the original array. That's a very different
operation, because it means any other references to the original array
won't see any change to the size (or any changes to the new array). If
the array object itself changed, the change would be visible through
all references.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

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

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
16
by: Roland Hall | last post by:
In working with arrays, I have found that I am unable to dimension and array with a variable that has an integer value but I can redimension one this way. I haven't see any information that tells...
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
13
by: DazedAndConfused | last post by:
Is there a quick way to fill an array with zeros or make all elements empty/nothing?
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
3
by: Jason | last post by:
Can someoneillustrate how I would open a text file that looks something similar to this: namea,8888,1234.99 nameb,8887,1242.50 and then after opening that file read each one of the values into...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.