tags:

views:

14335

answers:

7

I have an array of Foo objects and i need to remove the second element of the array

i need something similar to RemoveAt but for a regular array.

A: 

I'm not shore but I think you need to copy the elements you want to maintain to another array.

EDIT: I agree with the rest of the answers. If you want to have an array that changes frequently you should use another structure. Some sort of List.

Megacan
A: 

Here's how I did it...

 public static ElementDefinitionImpl[] RemoveElementDefAt(
  ElementDefinition[] oldList,
  int removeIndex
 )
 {
  ElementDefinitionImpl[] newElementDefList = new ElementDefinitionImpl[ oldList.Length - 1 ];

  int offset = 0;
  for ( int index = 0; index < oldList.Length; index++ )
  {
   ElementDefinitionImpl elementDef = oldList[ index ] as ElementDefinitionImpl;
   if ( index == removeIndex )
   {
    // This is the one we want to remove, so we won't copy it.  But 
    // every subsequent elementDef will by shifted down by one.
    offset = -1;
   }
   else
   {
    newElementDefList[ index + offset ] = elementDef;
   }
  }
  return newElementDefList;
 }
Paul Mitchell
+17  A: 

The nature of arrays is that their length is immutable. You can't add or delete any of the array items.

You will have to create a new array that is one element shorter and copy the old items to the new array, excluding the element you want to delete.

So it is probably better to use a List instead of an array.

Sebastian Dietz
i would if i could but i am getting data as an array
ooo
A: 

In a normal array you have to shuffle down all the array entries above 2 and then resize it using the Resize method. You might be better off using an ArrayList.

gkrogers
A: 

use System.Collections.ObjectModel.Collection<Foo>

abatishchev
+6  A: 

If you don't want to use List:

var foos = new List<Foo>(array);
foos.RemoveAt(index);
return foos.ToArray();

You could try this extension method that I haven't actually tested:

public static T[] RemoveAt<T>(this T[] source, int index)
{
    T[] dest = new T[array.Length - 1];
    if( index > 0 )
     Array.Copy(source, 0, dest, 0, index);

    if( index < source.Length - 1 )
     Array.Copy(source, index + 1, dest, index, source.Length - index - 1);

    return dest;
}

And use it like:

Foo[] bar = GetFoos();
bar = bar.RemoveAt(2);
Andrew Kennan
The first example given in this answer is much less efficient than the second. It requires two array copies and a shift of everything after index rather than one selective array copy.
Martin Brown
+1 of course, but we can also use list too ORList<Foo> list = new List<Foll>(GetFoos());list.Remove(my_foo);list.RemoveAt(2);where GetFoos() will return the array of Foos !!!!
Tumbleweed
First line inside the method should say 'source.Length' instead of 'array.Length'.
Nelson
+1  A: 

Here is an old version I have that works on V1.0 of the .Net framework and does not need Generic Types.

    public static Array RemoveAt(Array source, int index)
    {
        if (source == null)
            throw new ArgumentNullException("source");

        if (0 > index || index >= source.Length)
            throw new ArgumentOutOfRangeException("index", index, "index is outside the bounds of source array");

        Array dest = Array.CreateInstance(source.GetType().GetElementType(), source.Length - 1);
        Array.Copy(source, 0, dest, 0, index);
        Array.Copy(source, index + 1, dest, index, source.Length - index - 1);

        return dest;
    }

This is used like this:

class Program
{
    static void Main(string[] args)
    {
        string[] x = new string[20];
        for (int i = 0; i < x.Length; i++)
            x[i] = (i+1).ToString();

        string[] y = (string[])MyArrayFunctions.RemoveAt(x, 3);

        for (int i = 0; i < y.Length; i++)
            Console.WriteLine(y[i]);
    }
}
Martin Brown