如何删除数组中的第一个元素? [英] How to remove the first element in an array?
本文介绍了如何删除数组中的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组:
arr[0]="a"
arr[1]="b"
arr[2]="a"
我只想删除arr[0]
,并且保留 arr[1]
和arr[2]
.
我正在使用:
I want to remove only arr[0]
, and keep arr[1]
and arr[2]
.
I was using:
arr= arr.Where(w => w != arr[0]).ToArray();
由于 arr[0]
和 arr[2]
具有相同的值(a"),所以我得到的结果只是 arr[1]代码>.
Since arr[0]
and arr[2]
have the same value ("a"), the result I'm getting is only arr[1]
.
如何同时返回arr[1]
和arr[2]
,而只删除arr[0]
?
How can I return both arr[1]
and arr[2]
, and only remove arr[0]
?
推荐答案
您可以使用 Skip
轻松做到这一点:
You can easily do that using Skip
:
arr = arr.Skip(1).ToArray();
这会创建另一个数组,其中包含其他答案中的新元素.这是因为您无法从数组中删除或添加元素.数组有固定大小.
This creates another array with new elements like in other answers. It's because you can't remove from or add elements to an array. Arrays have a fixed size.
这篇关于如何删除数组中的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文