如何删除数组中的第一个元素? [英] How to remove the first element in an array?

查看:798
本文介绍了如何删除数组中的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

arr[0]="a"  
arr[1]="b"  
arr[2]="a"  

我只想删除,并保留 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆