2011-11-03 65 views
2

可能重複:
Delete row of 2D string array in C#如何從c#中的2d數組中刪除一行?

我有一個二維字符串數組,我想從數組中刪除指定的行。

+9

你不能從數組中「刪除」項目......它們的大小是固定的。你必須實例化一個較短的新陣列並複製項目以保持它。 –

+2

http://stackoverflow.com/questions/5376279/delete-row-of-2d-string-array-in-c-sharp –

+2

如果你想刪除項目,然後我建議使用列表

回答

1

如上所述,你不能從數組中刪除。

如果您需要經常刪除行,那麼可以將使用2d數組更改爲包含字符串數組的列表。這樣你可以使用列出實現的remove方法。

4
string[] a = new string[] { "a", "b" }; //dummy string array 
int deleteIndex = 1; //we want to "delete" element in position 1 of string 
a = a.ToList().Where(i => !a.ElementAt(deleteIndex).Equals(i)).ToArray(); 

髒,但給人的預期結果(foreach通過陣列進行測試)

編輯錯過了「二維數組」的細節,這裏是工作

string[][] a = new string[][] { 
     new string[] { "a", "b" } /*1st row*/, 
     new string[] { "c", "d" } /*2nd row*/, 
     new string[] { "e", "f" } /*3rd row*/ 
    }; 
    int rowToRemove = 1; //we want to get rid of row {"c","d"} 
    //a = a.ToList().Where(i => !i.Equals(a.ElementAt(rowToRemove))).ToArray(); //a now has 2 rows, 1st and 3rd only. 
    a = a.Where((el, i) => i != rowToRemove).ToArray(); // even better way to do it maybe 
正確的代碼

代碼更新

+0

非常光滑,我不會想到這樣做 – psubsee2003

+0

'ToList()'調用完全沒有必要。另外,你的例子中的'a'是數組,儘量避免'ElementAt()',如果可以的話,並使用直接索引。 –

+0

代碼更新和清理 – Alex

0

而不是陣列喲你可以使用List或ArrayList類。使用它你可以動態地添加元素並根據你的需求刪除。數組的大小是固定的,不能動態操作。

0

最好的方法是使用List<Type>!項目按添加到列表中的方式進行排序,每個項目都可以刪除。

像這樣:

var items = new List<string>; 
items.Add("One"); 
items.Add("Two"); 
items.RemoveAt(1); 
1

好了,所以我說你不能 「刪除」 他們。這仍然是事實。您必須創建一個具有足夠空間的新數組實例,以便保留要複製的項目。

如果這是一個參差不齊的數組,那麼在這裏使用LINQ可以簡化這一點。

string[][] arr2d = 
{ 
    new[] { "foo" }, 
    new[] { "bar", "baz" }, 
    new[] { "qux" }, 
}; 

// to remove the second row (index 1) 
int rowToRemove = 1; 
string[][] newArr2d = arr2d 
    .Where((arr, index) => index != rowToRemove) 
    .ToArray(); 

// to remove multiple rows (by index) 
HashSet<int> rowsToRemove = new HashSet<int> { 0, 2 }; 
string[][] newArr2d = arr2d 
    .Where((arr, index) => !rowsToRemove.Contains(index)) 
    .ToArray(); 

你可以使用其他LINQ方法來刪除行更容易(例如,Skip()Take()TakeWhile()等)的範圍。

如果這是一個真正的二維(或其他多維)數組,那麼您將無法在此處使用LINQ,而必須手動完成,並且它會涉及更多。這仍然適用於鋸齒陣列。

string[,] arr2d = 
{ 
    { "foo", null }, 
    { "bar", "baz" }, 
    { "qux", null }, 
}; 

// to remove the second row (index 1) 
int rowToRemove = 1; 
int rowsToKeep = arr2d.GetLength(0) - 1; 
string[,] newArr2d = new string[rowsToKeep, arr2d.GetLength(1)]; 
int currentRow = 0; 
for (int i = 0; i < arr2d.GetLength(0); i++) 
{ 
    if (i != rowToRemove) 
    { 
     for (int j = 0; j < arr2d.GetLength(1); j++) 
     { 
      newArr2d[currentRow, j] = arr2d[i, j]; 
     } 
     currentRow++; 
    } 
} 

// to remove multiple rows (by index) 
HashSet<int> rowsToRemove = new HashSet<int> { 0, 2 }; 
int rowsToKeep = arr2d.GetLength(0) - rowsToRemove.Count; 
string[,] newArr2d = new string[rowsToKeep, arr2d.GetLength(1)]; 
int currentRow = 0; 
for (int i = 0; i < arr2d.GetLength(0); i++) 
{ 
    if (!rowsToRemove.Contains(i)) 
    { 
     for (int j = 0; j < arr2d.GetLength(1); j++) 
     { 
      newArr2d[currentRow, j] = arr2d[i, j]; 
     } 
     currentRow++; 
    } 
}