简体   繁体   English

删除多维字符串数组C#中的项目

[英]Remove Item in multidimensional string array c#

I has a multidimentional string array as below 我有一个多维字符串数组,如下所示

string[][] data = new string[3][]; string [] [] data =新的string [3] [];

the item in the string array as below 字符串数组中的项目如下

data[0]
    [0] "EUG5"  string
    [1] "FA1"   string

data[1]
    [0] "9.000000"  string
    [1] "1000"  string

data[2]
    [0] "1" string
    [1] "0" string

I wish to remove the data[0][1], data[1][1] and data[2][1], this is base on the condition on data[2] where it is "0". 我希望删除data [0] [1],data [1] [1]和data [2] [1],这是基于data [2]为“ 0”的条件。 Is it possible to do this? 是否有可能做到这一点?

You can use Array.Resize to do what you want to do. 您可以使用Array.Resize来完成您想做的事情。 Try this LINQpad script: 试试下面的LINQpad脚本:

var data = new string[][]{new []{"EUG5","FA1"},new []{"9.000000","1000"},new []{"1","0"}};

data.Dump();

Array.Resize(ref data[0],1);
Array.Resize(ref data[1],1);
Array.Resize(ref data[2],1);

data.Dump();

It produces this: 它产生此:

在此处输入图片说明

You can also use Array.Copy to move elements around, so this is the more generic case: 您还可以使用Array.Copy来移动元素,因此这是更通用的情况:

var data = new string[][]{new []{"EUG5","FA1"},new []{"9.000000","1000"},new []{"1","0"}};

data.Dump();

var colToDelete = 0;

for (int i = 0; i < data.Length; i++)
{
    Array.Copy(data[i],colToDelete+1,data[i],colToDelete,data[i].Length-colToDelete-1);
    Array.Resize(ref data[i],data[i].Length-1);
}

data.Dump();

but like the other posters correctly state, it's way easier with a List . 但是就像其他海报正确声明的那样 ,使用List更容易。

You cannot remove elements from an Array object in C#, all Arrays are immutable. 您无法从C#中的Array对象中删除元素,因为所有Array都是不可变的。 To get the flexibility you are looking for you want to use the List object. 为了获得灵活性,您正在寻找要使用List对象的方法。

If for some reason you are stuck and have to use arrays, then you could make a method that accepts an Array and Returns an Array. 如果由于某种原因您被卡住而不得不使用数组,那么您可以创建一个接受数组并返回数组的方法。 Then you could remove the objects like this: 然后,您可以删除这样的对象:

public string[][] RemoveElement(string[][] array, int coordinateA, int coordinateB)
{
    var ListOfItems = new List<List<string>>();

    foreach(string[] item in array)
        ListOfItems.add(new List<string>(item));

    ListOfItems[coordinateA].RemoveAt(coordinateB);

    var ReturnArray = new string[ListOfItems.Length][]();

    for(int i = 0; i < ListOfItems.Length; i++)
        ReturnArray[i] = ListOfItems[i].ToArray();

    return ReturnArray;
}

Of couse this is not a very optimized solution, but it will get the job done. 当然,这不是一个非常优化的解决方案,但可以完成工作。 There probably are extension method solutions for removing elements from arrays, but it would be far better to just use List object, and then return an array at the end of your code by calling List.ToArray() if it is necessary. 可能存在扩展方法解决方案,用于从数组中删除元素,但最好只使用List对象,然后在必要时通过调用List.ToArray()在代码末尾返回数组。

If you don't mind in creating new Array of Arrays instead of reusing existing array you could do this. 如果您不介意创建新Array of Arrays而不是重用现有数组,则可以这样做。

data = data
        .Select(x=> data[2][1] == "0"?   // Condition to filter
                x.Take(1).ToArray()      
                : x.ToArray())
        .ToArray();

Check this Demo 检查这个Demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM