删除常规数组的元素我有一个foo对象数组。如何删除数组的第二个元素?我需要类似的东西RemoveAt()但对于一个普通的数组。
3 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
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);
慕的地6264312
TA贡献1817条经验 获得超6个赞
MYYA
TA贡献1868条经验 获得超4个赞
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 关注
- 292 浏览
添加回答
举报
0/150
提交
取消