Modify an Array

  • Thread starter Thread starter Sorin Sandu
  • Start date Start date
S

Sorin Sandu

Is it posible to modify the lenghth of an array ?
I want to do something like
int i = 0;

string[] debit = new string[y];

string [] credit = new string[y];

while (myread3.Read())

{

debit = myread3.GetValue(0).ToString();

credit = myread3.GetValue(1).ToString();

i++;

}

But I don't know the value of y and I want to change it in the while loop.

How can I do that ?
 
Once an array has been created, its size cannot be adjusted. You would need
to create a new array and copy the elements. You could also use an
ArrayList.
 
I usually take the wimp's way out and use an ArrayList when I don't know the
array length in advance. Your code would look something like this:

int i = 0;
ArrayList debit = new ArrayList();
ArrayList credit = new ArrayList();
while (myRead3.Read())
{
debit.Add(myRead3.GetValue(0).ToString());
credit.Add(myRead3.GetValue(1).ToString());
i++;
}

You could also get rid of the int i = 0; and the i++; lines and just use the
foreach statement to iterate over the entire ArrayList collection, or use a
for loop that goes for (int i = 0; i < debit.Count; i++) if you need to keep
a count of the # of elements (the .Count property tells how many items you
have in the ArrayList, and elements are numbered from 0 up).

The downside, as far as I can tell, is that the ArrayList contains all
Objects. So it could use up more resources than other solutions, and you
may have to use the .ToString() method, or cast the elements back to
whatever type you are using when you retrieve them (it stores everything as
Objects, but remembers the type you used to store them; so it won't let you
perform an invalid cast on the ArrayList elements):

for (int i = 0; i < debit.Count; i++)
Console.WriteLine (debit.ToString());

Please excuse any typos, it's late.

Thanks,
Michael C., MCDBA
 
Sorin said:
Is it posible to modify the lenghth of an array ?
I want to do something like
int i = 0;

string[] debit = new string[y];

string [] credit = new string[y];

while (myread3.Read())

{

debit = myread3.GetValue(0).ToString();

credit = myread3.GetValue(1).ToString();

i++;

}

But I don't know the value of y and I want to change it in the while loop.

How can I do that ?

I think what you want is the ability to automatically expand the array?

int[] ints = new int[0];

for(int i = 0; i<10; i++)
{
int[] Temp = new int[ints.GetUpperBound(0)+1];
Temp.CopyTo(ints);
ints = i;
}

HTH

JB
 
for(int i = 0; i<10; i++)
{
int[] Temp = new int[ints.GetUpperBound(0)+1];
Temp.CopyTo(ints);
ints = i;
}


Allocating a new array each loop to increment the size by one? That's plain
bad. Just use an ArrayList (List<type> in .NET 2.0), it's what it's made
for.

Etienne Boucher
 
Carrying on from what Micheals said, if you want to return an array of a
specific type from an ArrayList
use the following.

return (MyType[])arrayListName.ToArray( typeof(MyType) );

Also, if you looking at storing a lot of string data in an array it's worth
taking a look at the System.Collections.Specialized namespace.

HTH

Glenn

Michael C said:
I usually take the wimp's way out and use an ArrayList when I don't know the
array length in advance. Your code would look something like this:

int i = 0;
ArrayList debit = new ArrayList();
ArrayList credit = new ArrayList();
while (myRead3.Read())
{
debit.Add(myRead3.GetValue(0).ToString());
credit.Add(myRead3.GetValue(1).ToString());
i++;
}

You could also get rid of the int i = 0; and the i++; lines and just use the
foreach statement to iterate over the entire ArrayList collection, or use a
for loop that goes for (int i = 0; i < debit.Count; i++) if you need to keep
a count of the # of elements (the .Count property tells how many items you
have in the ArrayList, and elements are numbered from 0 up).

The downside, as far as I can tell, is that the ArrayList contains all
Objects. So it could use up more resources than other solutions, and you
may have to use the .ToString() method, or cast the elements back to
whatever type you are using when you retrieve them (it stores everything as
Objects, but remembers the type you used to store them; so it won't let you
perform an invalid cast on the ArrayList elements):

for (int i = 0; i < debit.Count; i++)
Console.WriteLine (debit.ToString());

Please excuse any typos, it's late.

Thanks,
Michael C., MCDBA

Sorin Sandu said:
Is it posible to modify the lenghth of an array ?
I want to do something like
int i = 0;

string[] debit = new string[y];

string [] credit = new string[y];

while (myread3.Read())

{

debit = myread3.GetValue(0).ToString();

credit = myread3.GetValue(1).ToString();

i++;

}

But I don't know the value of y and I want to change it in the while loop.

How can I do that ?

 
Also, if the OP has access to the beta of .NET 2.0, I would recommend
using the List<T> generic type. This would give the best performance, and
would be the best option (don't have to worry about miscasts).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Glenn said:
Carrying on from what Micheals said, if you want to return an array of a
specific type from an ArrayList
use the following.

return (MyType[])arrayListName.ToArray( typeof(MyType) );

Also, if you looking at storing a lot of string data in an array it's
worth
taking a look at the System.Collections.Specialized namespace.

HTH

Glenn

Michael C said:
I usually take the wimp's way out and use an ArrayList when I don't know the
array length in advance. Your code would look something like this:

int i = 0;
ArrayList debit = new ArrayList();
ArrayList credit = new ArrayList();
while (myRead3.Read())
{
debit.Add(myRead3.GetValue(0).ToString());
credit.Add(myRead3.GetValue(1).ToString());
i++;
}

You could also get rid of the int i = 0; and the i++; lines and just use the
foreach statement to iterate over the entire ArrayList collection, or use a
for loop that goes for (int i = 0; i < debit.Count; i++) if you need to keep
a count of the # of elements (the .Count property tells how many items
you
have in the ArrayList, and elements are numbered from 0 up).

The downside, as far as I can tell, is that the ArrayList contains all
Objects. So it could use up more resources than other solutions, and you
may have to use the .ToString() method, or cast the elements back to
whatever type you are using when you retrieve them (it stores everything as
Objects, but remembers the type you used to store them; so it won't let you
perform an invalid cast on the ArrayList elements):

for (int i = 0; i < debit.Count; i++)
Console.WriteLine (debit.ToString());

Please excuse any typos, it's late.

Thanks,
Michael C., MCDBA

Sorin Sandu said:
Is it posible to modify the lenghth of an array ?
I want to do something like
int i = 0;

string[] debit = new string[y];

string [] credit = new string[y];

while (myread3.Read())

{

debit = myread3.GetValue(0).ToString();

credit = myread3.GetValue(1).ToString();

i++;

}

But I don't know the value of y and I want to change it in the while loop.

How can I do that ?


 
Etienne said:
for(int i = 0; i<10; i++)
{
int[] Temp = new int[ints.GetUpperBound(0)+1];
Temp.CopyTo(ints);
ints = i;
}



Allocating a new array each loop to increment the size by one? That's plain
bad. Just use an ArrayList (List<type> in .NET 2.0), it's what it's made
for.

Etienne Boucher

Why is that bad?
If it is not in a performance critical area, then what is the damage?
I think using a generic arraylist is worse.

List<type> would be better, yes but in the meantime I would rather
copyto than use an arraylist.

JB :)
 
Back
Top