0

I has a multidimentional string array as below

string[][] data = new 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". Is it possible to do this?

2
  • 1
    "Removing item" is impossible in array since its size is fixed. You can assign null or other values; but the place never vanishes. Use list instead. Commented Jun 21, 2016 at 2:54
  • if one of the answers below helped you out, refer to this: stackoverflow.com/help/someone-answers Commented Jul 3, 2016 at 20:33

3 Answers 3

2

You can use Array.Resize to do what you want to do. Try this LINQpad script:

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:

enter image description here

You can also use Array.Copy to move elements around, so this is the more generic case:

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You cannot remove elements from an Array object in C#, all Arrays are immutable. To get the flexibility you are looking for you want to use the List object.

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.

Comments

0

If you don't mind in creating new Array of Arrays instead of reusing existing array you could do this.

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

Check this Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.