increase the size of the array

  • Thread starter Thread starter reb
  • Start date Start date
R

reb

Hi,

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

thanks
 
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.ToArray( typeof( AType ) );

Regards
Lee
 
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 said:
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.ToArray( typeof( AType ) );

Regards
Lee
Günter Prossliner said:
Use an ArrayList. Arrays always are fixed - size in C#

GP
 
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 said:
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 said:
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.ToArray( typeof( AType ) );

Regards
Lee
Günter Prossliner said:
Use an ArrayList. Arrays always are fixed - size in C#

GP

Hi,

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

thanks
 
Of caurse, what means small? But image following code:

class MyClass{
string[] lines;

public MyClass(Stream content){

StreamReader sr = new StreamReader(content);

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

sr.Close();
}

public void AddLine(string line){
stirng[] copy = new string[lines.lenght];
lines.CopyTo(copy,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(content);

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

sr.Close();

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

It will be executed only once.


GP

Lee Alexander said:
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 said:
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 said:
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.ToArray( typeof( AType ) );

Regards
Lee
Use an ArrayList. Arrays always are fixed - size in C#

GP

Hi,

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

thanks
 
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 said:
Of caurse, what means small? But image following code:

class MyClass{
string[] lines;

public MyClass(Stream content){

StreamReader sr = new StreamReader(content);

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

sr.Close();
}

public void AddLine(string line){
stirng[] copy = new string[lines.lenght];
lines.CopyTo(copy,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(content);

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

sr.Close();

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

It will be executed only once.


GP

Lee Alexander said:
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 said:
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

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.ToArray( typeof( AType ) );

Regards
Lee
Use an ArrayList. Arrays always are fixed - size in C#

GP

Hi,

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

thanks
 
Back
Top