9

How to delete a specific row and column from 2D array in C#?

int[,] array= {{1,2,3},{4,5,6},{7,8,9}};

lets say I want to delete row i and column i (skipping them) ... for nXn array not just 3x3 and store the remaining array in a new array... so the output would be:

{5,6},{8,9}
5
  • 2
    what is your expected output? Commented Oct 10, 2014 at 15:37
  • possible duplicate of Delete row of 2D string array in C# Commented Oct 10, 2014 at 15:37
  • You can't "delete" items from an array, they are fixed in size. Commented Oct 10, 2014 at 15:39
  • You cannot delete item from array. but you can create a new one, which will contain all items from original array except items you want to be deleted. Commented Oct 10, 2014 at 15:40
  • Yes, I mentioned that I want to store it in a different array not the same one Commented Oct 10, 2014 at 15:43

4 Answers 4

9

There's no built-in way to do that, you can do it yourself:

 static void Main()
        {
            int[,] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            var trim = TrimArray(0, 2, array);
        }


        public static int[,] TrimArray(int rowToRemove, int columnToRemove, int[,] originalArray)
        {
            int[,] result = new int[originalArray.GetLength(0) - 1, originalArray.GetLength(1) - 1];

            for (int i = 0, j = 0; i < originalArray.GetLength(0); i++)
            {
                if (i == rowToRemove)
                    continue;

                for (int k = 0, u = 0; k < originalArray.GetLength(1); k++)
                {
                    if (k == columnToRemove)
                        continue;

                    result[j, u] = originalArray[i, k];
                    u++;
                }
                j++;
            }

            return result;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

@WT86 You can easily extract a method out of it, see the edited answer.
2

Very simple logic. Just play with the loop:

int[,] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[,] arrayskip = new int[array.GetLength(0) - 1, array.GetLength(1) - 1];

for (int i = 1; i < array.GetLength(0); i++)
{
    for (int j = 1; j < array.GetLength(1); j++)
    {
        arrayskip[i - 1, j - 1] = array[i, j];
    }
}

3 Comments

Thank you but in your case, if i-0, I assume an exception will be thrown since the index would be -1,-1 ? and that doesn't work.
Then the loop will not continue because length will be 0.
look at your condition array[i, j]; on first iteration how will you get array[0,0] element of array?
1

No, arrays don't let you do that. You could make your own data structure for that, but it's not going to be exactly simple (unlike if you only wanted to be able to remove rows, for example).

For simple operations, it would be quite enough to build a class on top of an underlying array, and handle the re-indexing to map the virtual 2D array to the physical array underneath. But it's going to get a bit tricky as you combine removals and additions, and deform the array overall.

3 Comments

OP asked to skip some items and store it in a new array. Not actually asked for deleting an array's item.
it's not like real deleting it's just skipping specific elements and store the remaining in a new array... that's it
@WT86 Well, in that case, a simple for-cycle will do. So what is your question, and where's your work? :D
0

I created this method, have a look

 public static double[,] fillNewArr(double[,] originalArr, int row, int col)
    {
        double[,] tempArray = new double[originalArr.GetLength(0) - 1, originalArr.GetLength(1) - 1];
        int newRow = 0;
        int newCol = 0;
        for (int i = 0; i < originalArr.GetLength(0); i++)
        {
            for (int j = 0; j < originalArr.GetLength(1); j++)
            {
                if(i != row && j != col)
                {
                    tempArray[newRow, newCol] = originalArr[i, j];
                    newRow++;
                    newCol++;
                }

            }

        }
        return tempArray;

    }

having some out of range, It's obvious why but I'm trying to get there...

4 Comments

You'll get an IndexOutOfRangeException.
yes you are right. I'm decreasing the array length by 1 and then adding the element I want in a non defined index!
@brz I modified it but still out of range. I have to place newRow++ in a better place..
Use my answer, I made it into a method.

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.