Insert item in array (C#)

July 21, 2005 at 21:38:05
Specs: Windows XP Home SP2, 2.8 GHz/448 MB

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


See More: Insert item in array (C#)

Report •


#1
July 22, 2005 at 12:09:16

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


Report •

#2
July 22, 2005 at 18:46:36

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


Report •

#3
July 22, 2005 at 19:59:42

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


Report •

Related Solutions

#4
July 22, 2005 at 21:42:40

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! :@



Report •

#5
July 23, 2005 at 11:22:39

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


Report •

#6
July 23, 2005 at 16:02:52

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


Report •

#7
July 23, 2005 at 23:21:10

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


Report •

#8
July 24, 2005 at 11:17:14

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


Report •

#9
July 25, 2005 at 20:48:21

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


Report •

#10
July 25, 2005 at 21:41:30

"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


Report •

#11
July 27, 2005 at 12:08:33

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


Report •


Ask Question