How to delete a row from a 2d array in c#?

c#
13,140

Solution 1

string[] a = new string[] { "a", "b" }; //dummy string array
int deleteIndex = 1; //we want to "delete" element in position 1 of string
a = a.ToList().Where(i => !a.ElementAt(deleteIndex).Equals(i)).ToArray();

dirty but gives the expected result (foreach through the array to test it)

EDIT missed the "2d array" detail, here is the right code for the job

    string[][] a = new string[][] { 
        new string[] { "a", "b" } /*1st row*/, 
        new string[] { "c", "d" } /*2nd row*/, 
        new string[] { "e", "f" } /*3rd row*/
    };
    int rowToRemove = 1; //we want to get rid of row {"c","d"}
    //a = a.ToList().Where(i => !i.Equals(a.ElementAt(rowToRemove))).ToArray(); //a now has 2 rows, 1st and 3rd only.
    a = a.Where((el, i) => i != rowToRemove).ToArray(); // even better way to do it maybe

code updated

Solution 2

As has been said above you cant remove from an array.

If you are going to need to remove rows quite often maybe change from using a 2d array to a list containing an array of string. This way you can make use of the remove methods that list implements.

Solution 3

Ok so I said you can't "delete" them. That's still true. You'll have to create a new array instance with enough space for the items you want to keep and copy them over.

If this is a jagged array, using LINQ here could simplify this.

string[][] arr2d =
{
    new[] { "foo" },
    new[] { "bar", "baz" },
    new[] { "qux" },
};
// to remove the second row (index 1)
int rowToRemove = 1;
string[][] newArr2d = arr2d
    .Where((arr, index) => index != rowToRemove)
    .ToArray();
// to remove multiple rows (by index)
HashSet<int> rowsToRemove = new HashSet<int> { 0, 2 };
string[][] newArr2d = arr2d
    .Where((arr, index) => !rowsToRemove.Contains(index))
    .ToArray();

You could use other LINQ methods to remove ranges of rows easier (e.g., Skip(), Take(), TakeWhile(), etc.).

If this is a true two-dimensional (or other multi-dimensional) array, you won't be able to use LINQ here and will have to do it by hand and it gets more involved. This still applies to the jagged array as well.

string[,] arr2d =
{
    { "foo", null },
    { "bar", "baz" },
    { "qux", null },
};
// to remove the second row (index 1)
int rowToRemove = 1;
int rowsToKeep = arr2d.GetLength(0) - 1;
string[,] newArr2d = new string[rowsToKeep, arr2d.GetLength(1)];
int currentRow = 0;
for (int i = 0; i < arr2d.GetLength(0); i++)
{
    if (i != rowToRemove)
    {
        for (int j = 0; j < arr2d.GetLength(1); j++)
        {
            newArr2d[currentRow, j] = arr2d[i, j];
        }
        currentRow++;
    }
}
// to remove multiple rows (by index)
HashSet<int> rowsToRemove = new HashSet<int> { 0, 2 };
int rowsToKeep = arr2d.GetLength(0) - rowsToRemove.Count;
string[,] newArr2d = new string[rowsToKeep, arr2d.GetLength(1)];
int currentRow = 0;
for (int i = 0; i < arr2d.GetLength(0); i++)
{
    if (!rowsToRemove.Contains(i))
    {
        for (int j = 0; j < arr2d.GetLength(1); j++)
        {
            newArr2d[currentRow, j] = arr2d[i, j];
        }
        currentRow++;
    }
}
Share:
13,140

Related videos on Youtube

user1027129
Author by

user1027129

Updated on June 04, 2022

Comments

  • user1027129
    user1027129 over 1 year

    Possible Duplicate:
    Delete row of 2D string array in C#

    i have a 2d string array, I want to delete a specified row from the array.

    • Jeff Mercado
      Jeff Mercado almost 12 years
      You can't "delete" items from an array... they are fixed in size. You'll have to instantiate a new array one row shorter and copy the items to keep to it.
    • Haim Evgi
      Haim Evgi almost 12 years
    • Bali C
      Bali C almost 12 years
      If you want to remove items then I suggest using a List<T>
    • Alex
      Alex almost 12 years
      posted working snippet below in my answer, regards
  • psubsee2003
    psubsee2003 almost 12 years
    Very slick, I wouldn't have thought to do that
  • Jeff Mercado
    Jeff Mercado almost 12 years
    The ToList() call there is totally unnecessary. Also, a in your examples are arrays, try to avoid ElementAt() if you can and use direct indexing.