473,414 Members | 1,723 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,414 software developers and data experts.

Modify an Array

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[i] = myread3.GetValue(0).ToString();

credit[i] = 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 ?
Nov 16 '05 #1
8 2143
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.

"Sorin Sandu" <ss****@yahoo.com.del> wrote in message
news:ep*************@TK2MSFTNGP14.phx.gbl...
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[i] = myread3.GetValue(0).ToString();

credit[i] = 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 ?

Nov 16 '05 #2
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[i].ToString());

Please excuse any typos, it's late.

Thanks,
Michael C., MCDBA

"Sorin Sandu" <ss****@yahoo.com.del> wrote in message
news:ep*************@TK2MSFTNGP14.phx.gbl...
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[i] = myread3.GetValue(0).ToString();

credit[i] = 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 ?

Nov 16 '05 #3
Sorin Sandu wrote:
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[i] = myread3.GetValue(0).ToString();

credit[i] = 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] = i;
}

HTH

JB
Nov 16 '05 #4
> for(int i = 0; i<10; i++)
{
int[] Temp = new int[ints.GetUpperBound(0)+1];
Temp.CopyTo(ints);
ints[i] = 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
Nov 16 '05 #5
Thnak's to all for response.
I will use an ArrayList.
"Sorin Sandu" <ss****@yahoo.com.del> a scris în mesajul de
ºtiri:ep*************@TK2MSFTNGP14.phx.gbl...
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[i] = myread3.GetValue(0).ToString();

credit[i] = 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 ?

Nov 16 '05 #6
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" <mi******@nospam.org> wrote in message
news:Bt*********************@news4.srv.hcvlny.cv.n et...
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[i].ToString());

Please excuse any typos, it's late.

Thanks,
Michael C., MCDBA

"Sorin Sandu" <ss****@yahoo.com.del> wrote in message
news:ep*************@TK2MSFTNGP14.phx.gbl...
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[i] = myread3.GetValue(0).ToString();

credit[i] = 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 ?


Nov 16 '05 #7
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]
- mv*@spam.guard.caspershouse.com

"Glenn" <cs****@blackwinter.net> wrote in message
news:uE*************@TK2MSFTNGP11.phx.gbl...
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" <mi******@nospam.org> wrote in message
news:Bt*********************@news4.srv.hcvlny.cv.n et...
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[i].ToString());

Please excuse any typos, it's late.

Thanks,
Michael C., MCDBA

"Sorin Sandu" <ss****@yahoo.com.del> wrote in message
news:ep*************@TK2MSFTNGP14.phx.gbl...
> 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[i] = myread3.GetValue(0).ToString();
>
> credit[i] = 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 ?
>
>



Nov 16 '05 #8
Etienne Boucher wrote:
for(int i = 0; i<10; i++)
{
int[] Temp = new int[ints.GetUpperBound(0)+1];
Temp.CopyTo(ints);
ints[i] = 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 :)
Nov 16 '05 #9

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

Similar topics

7
by: knocte | last post by:
Hello. I am using a foreach statement to read an array. But at some point, I want to change the value of the array, but it doesn't work. My code is: if ($aProvisions) foreach($aProvisions as...
12
by: Venkat | last post by:
Greetings to All, I have the following file and i need to modify it with the contents of an array File1.txt Name Location Points Grade Venkat,Newyork,100,A Jack,LA,12,C
28
by: Charles Sullivan | last post by:
I'm working on a program which has a "tree" of command line arguments, i.e., myprogram level1 ]] such that there can be more than one level2 argument for each level1 argument and more than one...
13
by: baumann.Pan | last post by:
when define char *p = " can not modify"; p ='b' ;is not allowed, but if you declare p as char p = "can modify"; p = 'b'; is ok? why?
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
6
by: Kiran | last post by:
Hi all, What I am trying to do is to pass a pointer to the first element of an array to a function, modify it in that function, and then print out the values of the array (which has been modified...
5
by: kidders | last post by:
Below is a script i need to modify to work and its driving me nuts. Essentially it fades the content of an array. The original script specified the object name, I tried to modify it to allow an...
8
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type --...
3
FLEB
by: FLEB | last post by:
I'm creating a couple helper methods to beef up the Array type: Array.shuffle() and Array.rotate(steps) (rotates an array like , , , etc). What I'm wondering is what the consensus is on modifying...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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,...

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.