473,785 Members | 2,841 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I resize an array?

Hi,

I declared an array like this:

string[] scriptArgs = new string[10];

Can I resize the array later in the code?

Thank you,
Max
Apr 12 '06 #1
12 14457
No.

Use Systems.Collect ions.ArrayList instead.
Then read a good C# book.

-dm

Apr 12 '06 #2
Although ArrayList may be preferable in some cases, you don't need to choose
it just because you want to resize easily. Arrays are also easily resized.

With C# 2.0 simply use Array.Resize (for one-dimensional arrays).

Prior to 2.0, use the method described here:
http://www.tangiblesoftwaresolutions...20Preserve.htm
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Maxwell200 6" wrote:
Hi,

I declared an array like this:

string[] scriptArgs = new string[10];

Can I resize the array later in the code?

Thank you,
Max

Apr 12 '06 #3
Maxwell,
please keep in mind that
generaly resizing a fixed array is a process that is cpu coslty (Im not
to sure about c# 2.0).

It generaly involves making a new array then copy everything from the
old array into it, though this is greatly oversimplfing what actuly
happens.

An Arraylist is more effecient for arrays that will undergo resizing
operations on a regular basis. That is why it was provided.

-dm

Apr 12 '06 #4
David Anton <Da********@dis cussions.micros oft.com> wrote:
Although ArrayList may be preferable in some cases, you don't need to choose
it just because you want to resize easily. Arrays are also easily resized.

With C# 2.0 simply use Array.Resize (for one-dimensional arrays).


You need to be careful here. Arrays can't be resized in the same way
that Strings can't be changed. Array.Resize doesn't *actually* resize
the array any more than String.Replace replaces occurrences in the
string it's called on. Instead, Array.Resize returns a *new* array of
the desired size, with the elements from the original array copied into
it. Now, if you've only got one reference to the array, that's fine -
but if you've got two variables which refer to the original array,
calling Array.Resize will have no effect as far as the "extra" variable
is concerned. Here's an example:

using System;

class Test
{
static void Main()
{
string[] x = {"hello", "there"};
string[] y = x;

Array.Resize(re f x, 3);

Console.WriteLi ne (x.Length);
Console.WriteLi ne (y.Length);
}
}

(I'm sure you knew all this David - I just wanted to be clear for the
OP.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 12 '06 #5
Good point Jon - it's something you definitely need to be aware of.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Jon Skeet [C# MVP]" wrote:
David Anton <Da********@dis cussions.micros oft.com> wrote:
Although ArrayList may be preferable in some cases, you don't need to choose
it just because you want to resize easily. Arrays are also easily resized.

With C# 2.0 simply use Array.Resize (for one-dimensional arrays).


You need to be careful here. Arrays can't be resized in the same way
that Strings can't be changed. Array.Resize doesn't *actually* resize
the array any more than String.Replace replaces occurrences in the
string it's called on. Instead, Array.Resize returns a *new* array of
the desired size, with the elements from the original array copied into
it. Now, if you've only got one reference to the array, that's fine -
but if you've got two variables which refer to the original array,
calling Array.Resize will have no effect as far as the "extra" variable
is concerned. Here's an example:

using System;

class Test
{
static void Main()
{
string[] x = {"hello", "there"};
string[] y = x;

Array.Resize(re f x, 3);

Console.WriteLi ne (x.Length);
Console.WriteLi ne (y.Length);
}
}

(I'm sure you knew all this David - I just wanted to be clear for the
OP.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 12 '06 #6
Yes - the performance difference does become noticable at a certain point.
In some tests just now, the array resize becomes noticably slower at around
a few thousand resizes, varying depending on the size of the array (larger
arrays result in more expensive resizing).

If using an array doesn't provide any benefit to you over an ArrayList, then
this would be good reason to use ArrayList over an array.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"th*********@gm ail.com" wrote:
Maxwell,
please keep in mind that
generaly resizing a fixed array is a process that is cpu coslty (Im not
to sure about c# 2.0).

It generaly involves making a new array then copy everything from the
old array into it, though this is greatly oversimplfing what actuly
happens.

An Arraylist is more effecient for arrays that will undergo resizing
operations on a regular basis. That is why it was provided.

-dm

Apr 12 '06 #7
In addition ususally, ArrayList are more appropriate when you are storing
reference types. If you are storing value types (int, float, bool,
structures, etc.), then it is more appropriate to use arrays.

ArrayList stores elements as System.Object. If you add value types to an
ArrayList, boxing occurs to box the value type into a reference type
(System.Object) . When you access elements of an ArrayList, unboxing occurs
to unbox the reference type, back into a value type. The boxing/unboxing
process is costly. A simple test can show that storing value types in an
array is more efficient than in an ArrayList. If you are storing Reference
types, then it is more efficient to store it an ArrayList rather than an
array. ArrayList are not always to best choice.

"th*********@gm ail.com" wrote:
Maxwell,
please keep in mind that
generaly resizing a fixed array is a process that is cpu coslty (Im not
to sure about c# 2.0).

It generaly involves making a new array then copy everything from the
old array into it, though this is greatly oversimplfing what actuly
happens.

An Arraylist is more effecient for arrays that will undergo resizing
operations on a regular basis. That is why it was provided.

-dm

Apr 13 '06 #8
> ArrayList stores elements as System.Object. If you add value types to an
ArrayList, boxing occurs to box the value type into a reference type
(System.Object) . When you access elements of an ArrayList, unboxing occurs
to unbox the reference type, back into a value type. The boxing/unboxing
process is costly.
In .NET 1.1, (almost) all collection structures store Objects, and so
they box and unbox value types accordingly. In .NET 2.0, there is
List<T>, which stores value types natively and so performs no boxing or
unboxing if it is created for a value type.

Even in .NET 1.1, though, boxing and unboxing are rarely so noticeable
that it's worth warping your design if an ArrayList was the "right
choice" from a design standpoint. Always choose the most appropriate
data structure for your design and then profile your app to see if
things like boxing cause you a real performance problem. Adapt your
design only if this is so. Otherwise you get tortured code in the name
of "efficiency " in spots where if you did it the nice (but
"inefficien t") way it would have almost no effect on your program
whatsoever.
[Resizing an array] generally involves making a new array then copy everything from the old array into it.


So far as I know (and I'm often wrong) this is exactly what happens
with an ArrayList. ArrayList uses an array as its backing
implementation, so when you fill your ArrayList it simply allocates
another array and copies the contents from one to the other. I believe
that the size of the backing array doubles on each reallocation.

Of course, I could be completely out to lunch. Anyone know if this is
so?

Apr 13 '06 #9


"Bruce Wood" wrote:
ArrayList stores elements as System.Object. If you add value types to an
ArrayList, boxing occurs to box the value type into a reference type
(System.Object) . When you access elements of an ArrayList, unboxing occurs
to unbox the reference type, back into a value type. The boxing/unboxing
process is costly.
In .NET 1.1, (almost) all collection structures store Objects, and so
they box and unbox value types accordingly. In .NET 2.0, there is
List<T>, which stores value types natively and so performs no boxing or
unboxing if it is created for a value type.


Yes, List<T> is strongly typed based on what you give it. It is the
perfered way in .NET 2.0.
Even in .NET 1.1, though, boxing and unboxing are rarely so noticeable
that it's worth warping your design if an ArrayList was the "right
choice" from a design standpoint. Always choose the most appropriate
data structure for your design and then profile your app to see if
things like boxing cause you a real performance problem. Adapt your
design only if this is so. Otherwise you get tortured code in the name
of "efficiency " in spots where if you did it the nice (but
"inefficien t") way it would have almost no effect on your program
whatsoever.


I disagree about the performance hit of boxing/unboxing. For a small amount
of elements, there is a performance differents, but it is so small it is not
noticable. However, when you get into the thousands, and larger,
boxing/unboxing is noticable. I redesigned an application that stored pixel
values of a camera in an ArrayList (.NET 1.1) of floats. The number of
varies from (320*240, 640*480, and 1024 * 1024). Performing calculations on
these pixel values stored in an ArrayList took many seconds, and sometimes
minutes. I converted the ArrayLists to arrays of floats and the calculations
now only take a couple of seconds.

It really depends on the application. If you iterate through the array
once, the it doesn't make a difference. However, you have to access the
elements many times and interate through the arrays multiple times, then you
do notice a difference.
[Resizing an array] generally involves making a new array then copy everything from the old array into it.


So far as I know (and I'm often wrong) this is exactly what happens
with an ArrayList. ArrayList uses an array as its backing
implementation, so when you fill your ArrayList it simply allocates
another array and copies the contents from one to the other. I believe
that the size of the backing array doubles on each reallocation.

Of course, I could be completely out to lunch. Anyone know if this is
so?

Apr 13 '06 #10

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

Similar topics

1
3728
by: Dean | last post by:
First I've must say Im completly new in php scripting What I need to do is upload, resize pictures with path in database Here is theory of it, and plan of doing it Hope somebody can help me here are 4 sizes of images:
16
6211
by: machine99 | last post by:
how do you resize an array allocated with new?
2
3502
by: xain | last post by:
HtmlString includes a web page, and soon it is converted to Html file. In the web page, they are images, and some of them are large. They are so large, in fact they are going to destroy my Tables, so I need to resize these images. I write a Function to do this job. Resizeimage(HtmlString) Function Resizeimage(ConStr) Dim TempStr,Re,Matches,Match,Tempi,TempArray
2
2666
by: mrbrightsidestolemymoney | last post by:
Hi, I'm having a problem resizing a (very big) nested vector. It's not the most streamlined piece of code ever but I need this array to avoid having to recalculate the same quantity millions of times! The (relevant) snippets of code are below : since it's relevant though L=28,T=96 (so TasteProps weighs in at a hefty 8*96*28*28*28*96=1,618,477,056 doubles )
7
6443
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are now thown out of the array released properly by the CLI?
6
13918
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an instance for the copy constructor). For example: C++ vector<classvClass;
2
2125
by: David Sanders | last post by:
Hi, I have a script with function definitions which I load into ipython for interactive use. These functions modify a global numpy array, whose size I need to be able to change interactively. I thus have a script which looks like this: from numpy import *
2
1566
by: Damien582 | last post by:
Hi guys, new here. I've used this site for a while researching answers, but this is my first post. I've searched everywhere and I haven't found an answer that works. First, what I am doing: Cubed array (3 dimesions, not jagged) of a custom structure for a game I am making to hold tile information. I can resize a 2 dimensional layer just fine, but upon resizing a 3 dimensional array (via creating new array and using Array.Copy) I could not...
9
6185
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've an optimization question for you all really quick. I have a stream that I am reading some bytes. At times, the stream can contain a small amount of bytes such as 50 or so or it can contain as much 10000000 bytes. In reality, I do not know the maximum number of bytes. In my function, I am going to read() the byte stream using a buffer. Now, is it better to read it into a buffer and dump the buffer into a...
0
9480
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
10153
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...
1
10093
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8976
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...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.