如何删除二维数组中的一行和一列?

问题描述:

我有一个int阵列到dimentions:如何删除二维数组中的一行和一列?

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

我想有两种方法,例如:

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

所以,具有该阵列:

1 2 3 
4 5 6 
7 8 9 

调用使用lineToRemove = 1的removeLine方法我必须得到数组:

1 2 3 
7 8 9 

调用带有columnToRemove = 1 columnLine方法我得数组:

1 3 
4 6 
7 9 

你有什么建议的实施?

感谢

RT

因为数组不打算在其中添加&删除项目的情况下,你应该使用更合适的数据结构。

LinkedList是最好的一个使用,当你期望会有很多的增加和删除在中间或列表的开始。

列表工作正常,当你只是从最后添加和删除。

你会通过导入使用:

using System.Collections.Generic; 

和编码:

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

尼斯尖。我使用了泛型列表,算法很简单。 谢谢:)。 – Ricardo 2010-01-29 11:19:40

+0

性能应该会更好...... – 2010-01-29 12:18:32

数组的大小不是动态的,所以你必须创建一个新的数组,填写您要保留的值,并忽略要删除的人。

非常基本的伪代码:

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