回答
如上所述,你不能從數組中刪除。
如果您需要經常刪除行,那麼可以將使用2d數組更改爲包含字符串數組的列表。這樣你可以使用列出實現的remove方法。
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
正確的代碼
代碼更新
非常光滑,我不會想到這樣做 – psubsee2003
'ToList()'調用完全沒有必要。另外,你的例子中的'a'是數組,儘量避免'ElementAt()',如果可以的話,並使用直接索引。 –
代碼更新和清理 – Alex
而不是陣列喲你可以使用List或ArrayList類。使用它你可以動態地添加元素並根據你的需求刪除。數組的大小是固定的,不能動態操作。
最好的方法是使用List<Type>
!項目按添加到列表中的方式進行排序,每個項目都可以刪除。
像這樣:
var items = new List<string>;
items.Add("One");
items.Add("Two");
items.RemoveAt(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++;
}
}
- 1. 如何從C中的二維數組中刪除一行?
- 2. 在C++中刪除2d數組
- 3. 如何從c#中的多維數組中刪除空白行?
- 4. 在C中刪除2D字符串數組的行#
- 5. 從numpy的3d數組中刪除2d子數組
- 6. 如何從數組中刪除數組?
- 7. 如何從數據庫(C#)中的數組中刪除數據?
- 8. 如何從c中的數組中刪除數據#
- 9. 在java中刪除2D數組列表中的特定行
- 10. 如何刪除一個函數修改後的2D數組?
- 11. 如何從C#中的數組中刪除範圍
- 12. 如何從C中的char數組中刪除前導「http://」?
- 13. 如何從numpy數組列表中刪除一個numpy數組?
- 14. 如何從另一個數組中刪除數組元素?
- 15. 有沒有辦法在2d數組中刪除特定的行?
- 16. C#從類數組中刪除項目
- 17. 如何刪除數組C++?
- 18. 刪除數組中的一行
- 19. 如何從數組中刪除空值?
- 20. 如何從數組中刪除元素?
- 21. 如何從數組中刪除元素?
- 22. 如何從一個數組中的項目中刪除模式
- 23. 如何從iphone中的數組中刪除一個對象
- 24. 如何從cookie數組中刪除數組中的值?
- 25. 如何從PHP中的數組中刪除子數組?
- 26. 如何刪除二維數組中的一行和一列?
- 27. 如何從C#中的結構數組中刪除一個項目#
- 28. 如何從MySQL的JSON數組中刪除一個數字?
- 29. 如何從RichDataTextBox中刪除每一行?
- 30. 如何從datagrid中刪除一行
你不能從數組中「刪除」項目......它們的大小是固定的。你必須實例化一個較短的新陣列並複製項目以保持它。 –
http://stackoverflow.com/questions/5376279/delete-row-of-2d-string-array-in-c-sharp –
如果你想刪除項目,然後我建議使用列表 –