刪除常規(guī)數(shù)組的元素我有一個(gè)foo對象數(shù)組。如何刪除數(shù)組的第二個(gè)元素?我需要類似的東西RemoveAt()但對于一個(gè)普通的數(shù)組。
3 回答

holdtom
TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
var foos = new List<Foo>(array);foos.RemoveAt(index);return foos.ToArray();
public static T[] RemoveAt<T>(this T[] source, int index){ T[] dest = new T[source.Length - 1]; if( index > 0 ) Array.Copy(source, 0, dest, 0, index); if( index < source.Length - 1 ) Array.Copy(source, index + 1, dest, index, source.Length - index - 1); return dest;}
Foo[] bar = GetFoos();bar = bar.RemoveAt(2);

米琪卡哇伊
TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊

12345678_0001
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
private int[] RemoveIndices(int[] IndicesArray, int RemoveAt){ int[] newIndicesArray = new int[IndicesArray.Length - 1]; int i = 0; int j = 0; while (i < IndicesArray.Length) { if (i != RemoveAt) { newIndicesArray[j] = IndicesArray[i]; j++; } i++; } return newIndicesArray;}
- 3 回答
- 0 關(guān)注
- 407 瀏覽
添加回答
舉報(bào)
0/150
提交
取消