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

Home Posts Topics Members FAQ

increase the size of the array

reb
Hi,

How do i increase the size of the array without losing the
data of that array in c#?

thanks
Nov 15 '05 #1
6 12134
Use an ArrayList. Arrays always are fixed - size in C#

GP

"reb" <gb***@yahoo.co m> wrote in message
news:0b******** *************** *****@phx.gbl.. .
Hi,

How do i increase the size of the array without losing the
data of that array in c#?

thanks

Nov 15 '05 #2
Just to add the other way to do it is to reallocate the array to the new
size and copy the old arrays contents into the new one. Saying that I would
use the ArrayList approach anyway in a situation where I don't know the
number of elements in advance. If you have then finished the operation of
adding items to it then you can *convert* the ArrayList into a strongly type
array like so:

AType items[] =(AType[]) myArrayList.ToA rray( typeof( AType ) );

Regards
Lee
"Günter Prossliner" <g.**********@g mx.at> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...
Use an ArrayList. Arrays always are fixed - size in C#

GP

"reb" <gb***@yahoo.co m> wrote in message
news:0b******** *************** *****@phx.gbl.. .
Hi,

How do i increase the size of the array without losing the
data of that array in c#?

thanks


Nov 15 '05 #3
If you do this with small arrays, it's ok (I also do this in that case), but
if the Array rows image that the elements must be copied on each call. In
this case it would be better to use an ArrayList (or an own, strong typed,
collectionclass )

GP

"Lee Alexander" <lee@No_Spam_Pl ease_Digita.com > wrote in message
news:%2******** *******@TK2MSFT NGP09.phx.gbl.. .
Just to add the other way to do it is to reallocate the array to the new
size and copy the old arrays contents into the new one. Saying that I would use the ArrayList approach anyway in a situation where I don't know the
number of elements in advance. If you have then finished the operation of
adding items to it then you can *convert* the ArrayList into a strongly type array like so:

AType items[] =(AType[]) myArrayList.ToA rray( typeof( AType ) );

Regards
Lee
"Günter Prossliner" <g.**********@g mx.at> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...
Use an ArrayList. Arrays always are fixed - size in C#

GP

"reb" <gb***@yahoo.co m> wrote in message
news:0b******** *************** *****@phx.gbl.. .
Hi,

How do i increase the size of the array without losing the
data of that array in c#?

thanks



Nov 15 '05 #4
I guess the trouble is though what is the definition of small? I would err
on the side of type safety and only if a performance was shown to be a
problem would I consider an ArrayList being exposed. I know what you mean
though :-)

Regards
Lee
"Günter Prossliner" <g.**********@g mx.at> wrote in message
news:uh******** ********@TK2MSF TNGP10.phx.gbl. ..
If you do this with small arrays, it's ok (I also do this in that case), but if the Array rows image that the elements must be copied on each call. In
this case it would be better to use an ArrayList (or an own, strong typed,
collectionclass )

GP

"Lee Alexander" <lee@No_Spam_Pl ease_Digita.com > wrote in message
news:%2******** *******@TK2MSFT NGP09.phx.gbl.. .
Just to add the other way to do it is to reallocate the array to the new
size and copy the old arrays contents into the new one. Saying that I

would
use the ArrayList approach anyway in a situation where I don't know the
number of elements in advance. If you have then finished the operation of adding items to it then you can *convert* the ArrayList into a strongly

type
array like so:

AType items[] =(AType[]) myArrayList.ToA rray( typeof( AType ) );

Regards
Lee
"Günter Prossliner" <g.**********@g mx.at> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...
Use an ArrayList. Arrays always are fixed - size in C#

GP

"reb" <gb***@yahoo.co m> wrote in message
news:0b******** *************** *****@phx.gbl.. .
> Hi,
>
> How do i increase the size of the array without losing the
> data of that array in c#?
>
> thanks



Nov 15 '05 #5
Of caurse, what means small? But image following code:

class MyClass{
string[] lines;

public MyClass(Stream content){

StreamReader sr = new StreamReader(co ntent);

for(string line=sr.ReadLin e(); line != null; line=sr.ReadLin e())
AddLine(line);

sr.Close();
}

public void AddLine(string line){
stirng[] copy = new string[lines.lenght];
lines.CopyTo(co py,0);
copy[lines.lenght] = line;
lines = copy;
}
}

If you would read a file line-by-line into this class, it would not perform
good. In this case it would be better to use an ArrayList, but here it would
be ok:

class MyClass2{
string[] lines;

public MyClass2(Stream content){
ArrayList al = new ArrayList();
StreamReader sr = new StreamReader(co ntent);

for(string line=sr.ReadLin e(); line != null; line=sr.ReadLin e())
al.Add(line);

sr.Close();

lines = (string[])(al.ToArray(ty peof(string)));
}
}

It will be executed only once.
GP

"Lee Alexander" <lee@No_Spam_Pl ease_Digita.com > wrote in message
news:uE******** ******@TK2MSFTN GP10.phx.gbl...
I guess the trouble is though what is the definition of small? I would err
on the side of type safety and only if a performance was shown to be a
problem would I consider an ArrayList being exposed. I know what you mean
though :-)

Regards
Lee
"Günter Prossliner" <g.**********@g mx.at> wrote in message
news:uh******** ********@TK2MSF TNGP10.phx.gbl. ..
If you do this with small arrays, it's ok (I also do this in that case),

but
if the Array rows image that the elements must be copied on each call. In
this case it would be better to use an ArrayList (or an own, strong typed, collectionclass )

GP

"Lee Alexander" <lee@No_Spam_Pl ease_Digita.com > wrote in message
news:%2******** *******@TK2MSFT NGP09.phx.gbl.. .
Just to add the other way to do it is to reallocate the array to the new size and copy the old arrays contents into the new one. Saying that I

would
use the ArrayList approach anyway in a situation where I don't know the number of elements in advance. If you have then finished the operation of adding items to it then you can *convert* the ArrayList into a

strongly type
array like so:

AType items[] =(AType[]) myArrayList.ToA rray( typeof( AType ) );

Regards
Lee
"Günter Prossliner" <g.**********@g mx.at> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...
> Use an ArrayList. Arrays always are fixed - size in C#
>
> GP
>
> "reb" <gb***@yahoo.co m> wrote in message
> news:0b******** *************** *****@phx.gbl.. .
> > Hi,
> >
> > How do i increase the size of the array without losing the
> > data of that array in c#?
> >
> > thanks
>
>



Nov 15 '05 #6
I totally agree with you that's why I said in my original post:

"Saying that I would use the ArrayList approach anyway in a situation where
I don't know the
number of elements in advance"

Regards
Lee
"Günter Prossliner" <g.**********@g mx.at> wrote in message
news:eE******** ******@TK2MSFTN GP12.phx.gbl...
Of caurse, what means small? But image following code:

class MyClass{
string[] lines;

public MyClass(Stream content){

StreamReader sr = new StreamReader(co ntent);

for(string line=sr.ReadLin e(); line != null; line=sr.ReadLin e())
AddLine(line);

sr.Close();
}

public void AddLine(string line){
stirng[] copy = new string[lines.lenght];
lines.CopyTo(co py,0);
copy[lines.lenght] = line;
lines = copy;
}
}

If you would read a file line-by-line into this class, it would not perform good. In this case it would be better to use an ArrayList, but here it would be ok:

class MyClass2{
string[] lines;

public MyClass2(Stream content){
ArrayList al = new ArrayList();
StreamReader sr = new StreamReader(co ntent);

for(string line=sr.ReadLin e(); line != null; line=sr.ReadLin e())
al.Add(line);

sr.Close();

lines = (string[])(al.ToArray(ty peof(string)));
}
}

It will be executed only once.
GP

"Lee Alexander" <lee@No_Spam_Pl ease_Digita.com > wrote in message
news:uE******** ******@TK2MSFTN GP10.phx.gbl...
I guess the trouble is though what is the definition of small? I would err
on the side of type safety and only if a performance was shown to be a
problem would I consider an ArrayList being exposed. I know what you mean though :-)

Regards
Lee
"Günter Prossliner" <g.**********@g mx.at> wrote in message
news:uh******** ********@TK2MSF TNGP10.phx.gbl. ..
If you do this with small arrays, it's ok (I also do this in that case),
but
if the Array rows image that the elements must be copied on each call. In this case it would be better to use an ArrayList (or an own, strong typed, collectionclass )

GP

"Lee Alexander" <lee@No_Spam_Pl ease_Digita.com > wrote in message
news:%2******** *******@TK2MSFT NGP09.phx.gbl.. .
> Just to add the other way to do it is to reallocate the array to the new > size and copy the old arrays contents into the new one. Saying that
I would
> use the ArrayList approach anyway in a situation where I don't know

the > number of elements in advance. If you have then finished the
operation of
> adding items to it then you can *convert* the ArrayList into a

strongly type
> array like so:
>
> AType items[] =(AType[]) myArrayList.ToA rray( typeof( AType ) );
>
> Regards
> Lee
> "Günter Prossliner" <g.**********@g mx.at> wrote in message
> news:OO******** ******@TK2MSFTN GP12.phx.gbl...
> > Use an ArrayList. Arrays always are fixed - size in C#
> >
> > GP
> >
> > "reb" <gb***@yahoo.co m> wrote in message
> > news:0b******** *************** *****@phx.gbl.. .
> > > Hi,
> > >
> > > How do i increase the size of the array without losing the
> > > data of that array in c#?
> > >
> > > thanks
> >
> >
>
>



Nov 15 '05 #7

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

Similar topics

12
7887
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
8
2906
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way to load them all into memory. I've tried using vector pushback with reserving, but it was horribly slow. The current method I am using is upon opening the file and reading the number of values, resizing the vectors (I have 3, one for each data...
14
8521
by: Sameer | last post by:
Hello, i wish to read a file of int and store into an array dynamically... the size of memory allocated finally, should just be sufficeient to store n integers. I do not know the number of integers in the file... How should I go about creating the array int array dynamically ? the file is
8
10606
by: Tee | last post by:
Hi, How do we increase the size of array on runtime and preserve the previous data? I don't want to use ArrayList because the array could be a multi-dimension array. Thanks, Tee
1
7269
by: Mohan | last post by:
Hi, In my application, I need to increase the 2-dimensional array size dynamically. Please let me know the procedure to declare a dynamic 2-dimensional array like Single dimensional array, ArrayList. Thanks, Mohan
2
3508
by: yoshitha | last post by:
Hi Can any one tell me how to increase size of an array in C#.net with ASP.Net? its very urgent for me thanks yoshitha
18
1958
by: HYRY | last post by:
I want to join two mono wave file to a stereo wave file by only using the default python module. Here is my program, but it is much slower than the C version, so how can I increase the speed? I think the problem is at line #1, #2, #3. import wave import array lfile = wave.open(lfilename) rfile = wave.open(rfilename)
1
2067
by: Amit Sharma | last post by:
hi frndzzzzzz i want to know how we can increase the size of an Array. during execution of the program.
4
2032
by: =?Utf-8?B?R2F1cmF2?= | last post by:
Hello... I posted this a couple of weeks back, but could't get a reply, so trying again! I use the Setup project in C# to create a .msi file to be distributed to the clients. The size of .msi is 12 MB. This file works fine on all the target machines, but on the development machine every time I remove the existing .msi (from add/remove programs) and install a new version, the size increases by 13-14
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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
10325
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
10147
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
9950
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
6739
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?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.