【问题标题】:How to delete a row from a 2d array in c#? [duplicate]如何从 C# 中的二维数组中删除一行? [复制]
【发布时间】:2011-11-03 09:38:41
【问题描述】:

可能重复:
Delete row of 2D string array in C#

我有一个二维字符串数组,我想从数组中删除指定的行。

【问题讨论】:

  • 您不能从数组中“删除”项目...它们的大小是固定的。您必须实例化一个短行的新数组并复制项目以保留它。
  • 如果你想删除项目,那么我建议使用 List
  • 在我的回答中发布了工作 sn-p,问候

标签: c#


【解决方案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();

脏但给出了预期的结果(foreach通过数组来测试它)

EDIT 错过了“二维数组”的细节,这里是该工作的正确代码

    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

代码更新

【讨论】:

  • 非常漂亮,我没想到会这样做
  • 那里的ToList() 电话完全没有必要。此外,您的示例中的a 是数组,如果可以,请尽量避免使用ElementAt() 并使用直接索引。
【解决方案2】:

如上所述,您不能从数组中删除。

如果您需要经常删除行,可能会从使用二维数组更改为包含字符串数组的列表。这样您就可以使用 list 实现的 remove 方法。

【讨论】:

    【解决方案3】:

    好的,我说你不能“删除”它们。这仍然是真的。您必须创建一个具有足够空间的新数组实例来存放您想要保留的项目并将它们复制过来。

    如果这是一个交错数组,在这里使用 LINQ 可以简化这一点。

    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();
    

    您可以使用其他 LINQ 方法更轻松地删除行范围(例如,Skip()Take()TakeWhile() 等)。

    如果这是一个真正的二维(或其他多维)数组,您将无法在此处使用 LINQ,而必须手动操作,并且涉及更多。这仍然适用于锯齿状数组。

    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++;
        }
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用 List 或 ArrayList 类来代替数组。使用它,您可以根据您的要求动态添加和删除元素。数组大小固定,不能动态操作。

      【讨论】:

        【解决方案5】:

        最好的方法是使用List&lt;Type&gt;!项目按添加到列表中的方式排序,每个项目都可以删除。

        像这样:

        var items = new List<string>;
        items.Add("One");
        items.Add("Two");
        items.RemoveAt(1);
        

        【讨论】:

          猜你喜欢
          • 2016-11-27
          • 2010-12-20
          • 1970-01-01
          • 2014-12-05
          • 2023-03-31
          • 1970-01-01
          • 2010-10-22
          • 1970-01-01
          • 2017-11-06
          相关资源
          最近更新 更多