Ricardo
Ricardo

Reputation: 1025

How to remove a line and column in a two dimension array?

I have an int array with to dimentions:

int[,] intArray2D = new int[3,3];

I would like to have two methods such as:

int[,] removeLine(int[,] array, int arrayColumnsCount, int lineToRemove){...}
int[,] removeColumn(int[,] array, int arrayColumnsCount, int columnToRemove){...}

So, having the array:

1 2 3
4 5 6
7 8 9

Calling the removeLine method with lineToRemove = 1 I must get the array:

1 2 3
7 8 9

Calling the columnLine method with columnToRemove = 1 I must get the array:

1 3
4 6
7 9

What are your implementation advices?

Thanks

RT

Upvotes: 1

Views: 3147

Answers (3)

Rob Fonseca-Ensor
Rob Fonseca-Ensor

Reputation: 15621

Because arrays aren't intended for situation where you add & remove items, you should use a more appropriate data structure.

LinkedList is the best one to use when you expect there will be lots of adds and removes in the middle or the start of the list.

List works fine when you're only adding and removing from the end.

You'd use this by importing:

using System.Collections.Generic;

and coding:

LinkedList<LinkedList<int>> grid = new LinkedList<LinkedList<int>>()
grid.Add(new LinkedList<int>()); //repeat for each column / row

Upvotes: 3

Farinha
Farinha

Reputation: 18091

Very basic pseudocode:

removeLine(...) {
    create array with 1 less line
    loop through lines of original array
        if line index == lineToRemove
            continue loop

removeColumns(...) {
    create array with 1 less column
    loop through columns of original array
        if column index == columnToRemove
            continue loop

Upvotes: 2

Jean-Bernard Pellerin
Jean-Bernard Pellerin

Reputation: 12670

Array sizes aren't dynamic, so you'd have to create a new array, fill the values you are keeping, and disregard the ones you are removing.

Upvotes: 3

Related Questions