Computing.Net > Forums > Programming > Insert item in array (C#)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Insert item in array (C#)

Reply to Message Icon

Name: Leo the 28C (by Sulfurik)
Date: July 21, 2005 at 21:38:05 Pacific
OS: Windows XP Home SP2
CPU/Ram: 2.8 GHz/448 MB
Comment:

Hello everyone! :D
OK, I have a multidimensional array of ints. It has 2 dimensions. First dimension is about 10 items, and 2nd dimension is only 3. I need to insert an item in the first dimension at a specified index. How do I do this? I don't want to use an ArrayList. Thanks! ;)

http://www.boredsource.com/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: July 22, 2005 at 12:09:16 Pacific
Reply:

There's no magic way...You have to move all the items past the insertion point down one notch then insert your value. ArrayList does this for you.

If this is your array and you want to put '1' in at the front:
|2|3|4|null|

Move all the items down a notch:
|2|2|3|4|

then insert your item:
|1|2|3|4|

The fact that there are two dimensions doesn't change anything. Also, you should check first to see if the array is big enough to hold the new element. If not, you have to copy all the elements to a new array, then insert the new item. ArrayList does all that for you, which is why people like to use it:-) Why don't you want to use it?

Good luck,
-SN


0

Response Number 2
Name: Leo the 28C (by Sulfurik)
Date: July 22, 2005 at 18:46:36 Pacific
Reply:

Thanks! :D
But now, what would be a correct equation to insert the new value somewhere in the middle of the array? I tried many things, going up to down, down to up, nothing works! :( Can you give me an example of how to do this? Thanks! ;)

http://www.boredsource.com/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D


0

Response Number 3
Name: SN
Date: July 22, 2005 at 19:59:42 Pacific
Reply:

I don't understand what you're asking...equation to insert the new value somewhere in the middle? How would an equation do that? Like I said, you have to move all the items down a notch, then assign the new value wherever you want it.
a = array;
v = new value
j = index the new value should go into;

for (i = a.length; i > j; i--)
a[i] = a[i - 1];

a[j] = value;

Again, if the array isn't big enough to hold the new value, you have to make a copy.

Good luck,
-SN


0

Response Number 4
Name: Leo the 28C (by Sulfurik)
Date: July 22, 2005 at 21:42:40 Pacific
Reply:

Yeah, I tried what you said many times and in many different ways, but it gives me unwanted results! :(
Look at the last thing I tried:

x = the control variable
this.FileParams = the original array
temp = an array whose's lenght is FileParams's plus 1
where = the index in which the new value will be inserted at
ints = the new value

----------
int x = 0;

while (x < temp.GetLength(0))
{
if (x == where + 1)
{
temp[x, 0] = ints[0];
temp[x, 1] = ints[1];
temp[x, 2] = ints[2];
}
else
{
temp[x, 0] = this.FileParams[x, 0];
temp[x, 1] = this.FileParams[x, 1];
temp[x, 2] = this.FileParams[x, 2];
}
x++;
}
----------


See, that gives me an IndexOutOfRangeException, and I;ve tried many things such as doing x-1, but nothing works! Can you give me another idea? Thanks very much for helping me! ;)

http://www.boredsource.com/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

I'll hack you with Notepad! :@



0

Response Number 5
Name: SN
Date: July 23, 2005 at 11:22:39 Pacific
Reply:

The code you posted isn't anything like what I did, but it's fairly close to working.

It's throwing an indexoutofrange exception because on the last iteration of the while loop, x is one less than temp's length, which is one more than the last valid index in FileParams. (if temp has 10 elements, FileParams has 9, and the last iteration will access FileParams[9], which isn't valid.)

Here's a static function that corrects it. The trick is in knowing what element of the source you should be accessing.

private static int[,] insertIntoArray(int[,] source, int newIndex, int[] newValue)
{
int[,] temp = new int[source.GetLength(0) + 1, source.GetLength(1)];
for (int i = 0; i < temp.GetLength(0); i++)
{
int sourceIndex = i < newIndex ? i : i - 1;
if (i == newIndex)
{
temp[i, 0] = newValue[0];
temp[i, 1] = newValue[1];
temp[i, 2] = newValue[2];
}
else
{
temp[i, 0] = source[sourceIndex, 0];
temp[i, 1] = source[sourceIndex, 1];
temp[i, 2] = source[sourceIndex, 2];
}
}
return temp;
}

Good luck,
-SN


0

Related Posts

See More



Response Number 6
Name: Leo the 28C (by Sulfurik)
Date: July 23, 2005 at 16:02:52 Pacific
Reply:

THANK YOU VERY MUCH IT WORKS!!! :D
I didn't know about the ?: operator though...
Thanks! ;)

http://www.boredsource.com/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D


0

Response Number 7
Name: Leo the 28C (by Sulfurik)
Date: July 23, 2005 at 23:21:10 Pacific
Reply:

One more thing:
How would I delete an item from the array? Thanks! ;)

http://www.boredsource.com/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D


0

Response Number 8
Name: SN
Date: July 24, 2005 at 11:17:14 Pacific
Reply:

Same concept...

private static int[,] insertIntoArray(int[,] source, int newIndex, int[] newValue)
{
int[,] temp = new int[source.GetLength(0) - 1, source.GetLength(1)];
for (int i = 0; i < temp.GetLength(0); i++)
{
int sourceIndex = i < newIndex ? i : i + 1;
if (i != newIndex)
{
temp[i, 0] = newValue[0];
temp[i, 1] = newValue[1];
temp[i, 2] = newValue[2];
}
}
return temp;
}

I wouldn't actually do it this way in production code...it's too inefficient. Of course, in production code, I would inherit from CollectionBase and use arraylist.

Good luck,
-SN


0

Response Number 9
Name: Leo the 28C (by Sulfurik)
Date: July 25, 2005 at 20:48:21 Pacific
Reply:

No, I can't use ArrayLists because:
1. Too lazy to convert :P
2. It's gotta be an int array, since the ints have to be accessed fast, and with ArrayLists I'd have to convert to int each time I need it, and I can't do that... :S (Is there another way of using ArrayLists?)

And, sorry for bothering you, but... I don't get how to use the last piece of code you used... I don't understand how I could translate that to delete an item... Could you explain, please? Thanks! ;)

http://www.boredsource.com/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D


0

Response Number 10
Name: SN
Date: July 25, 2005 at 21:41:30 Pacific
Reply:

"Is there another way of using ArrayLists"
Yes. Microsoft recommends that you inherit from System.Collections.CollectionBase and make your own "IntCollection" class. That way there's no converting necessary. It would probably be faster than resizing the array every time you want to insert or delete an element. There is an example of how to do this Here. Depending on what the data in your 2-d array represents, you might just make a class to hold the three ints, and another class to hold a collection of them. That's how I would do it in real life.

For example, your 2-d array could be representing points in three-dimensional space. If that were the case, I would create a Point class with three members, x, y, and z. Then I would create a PointCollection class that inherits from System.CollectionBase that holds all the points I want to keep together. It would do everything your 2-d array does and more, but be much more maintainable, readable, and probably more efficient.

The last piece of code I gave you will return a new array with the item at the specified index deleted. You can delete the third parameter to the function since you obviously aren't giving any new values to be inserted. If you compare it to the insertion function, you'll see how it works. Again, the trick is in knowing which index of the source you should be reading from.

Good luck,
-SN


0

Response Number 11
Name: Leo the 28C (by Sulfurik)
Date: July 27, 2005 at 12:08:33 Pacific
Reply:

SN, sorry for making you write all that code, but you were right, I should have used ArrayLists... I did what you said, and it works alot better! :D

Thanks! ;)

http://www.boredsource.com/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Insert item in array (C#)

Inserting text in TextBox help (C#) www.computing.net/answers/programming/inserting-text-in-textbox-help-c/12833.html

Maximum in array www.computing.net/answers/programming/maximum-in-array/6590.html

Max & Min Values in Arrays (C) www.computing.net/answers/programming/max-amp-min-values-in-arrays-c/8620.html