删除常规数组的元素我有一个foo对象数组。如何删除数组的第二个元素?我需要类似的东西RemoveAt()但对于一个普通的数组。
3 回答
holdtom
TA贡献1805条经验 获得超10个赞
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贡献1998条经验 获得超6个赞
12345678_0001
TA贡献1802条经验 获得超5个赞
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 关注
- 413 浏览
添加回答
举报
0/150
提交
取消
