vb.net 从数组中删除第一个元素
一种答案是创建一个短一个元素的新数组。还有其他更简单的方法可以做到这一点吗?
One answer is to create a new array that is one element shorter. Are there any other simpler ways to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 LINQ 以非常简洁的代码生成结果:
您可能会有批评者说这很慢,您应该使用 Array.Copy 代替:
但是,我测试了两者的时间使用具有 1,000,000 个元素的整数数组的方法,发现 LINQ 花费了 29 毫秒,直接复制花费了 3 毫秒。除非您正在使用大量元素进行某种疯狂的数学运算,否则 LINQ 就很好,而且可读性更高。
You can use LINQ to produce your result in a very concise bit of code:
You may have detractors say that this is slow and that you should use
Array.Copy
instead:However, I tested the timings of both methods using an array of integers with 1,000,000 elements and found that LINQ took 29 milliseconds and the direct copy took 3 milliseconds. Unless you're doing some sort of crazy math with gazilions of elements then LINQ is fine and is far more readable.
这是在 vb.net 中删除数组第一个元素的一种方法。
您可以为此创建一个函数来删除数组的任意元素(有一个用于 for 循环的初始值的参数)。
Here is one way to remove the first element of an array in vb.net.
You could make a function for this to remove an arbitrary element of an array (Have a parameter for the initial value of the for loop).
结合 @xpda 和 @Enigmativity 的答案,观察到 Array.Copy 可以安全地用于复制回原始数组。引用 Array.Copy 方法的 msdn 页面:
下面是一个(扩展)子例程,它将删除任意类型数组中指定索引处的元素:
用法示例(假设数组以索引 0 开头):
Combining @xpda's and @Enigmativity's answers, observe that Array.Copy can be safely used to copy back to the original array. Quote from msdn page for Array.Copy Method:
Here is an (extension) subroutine that will remove element, at specified index, of an array of any type:
Usage examples (assuming array starting with index 0):
您还可以转换列表,删除特定索引处的项目,然后将其转换回数组。
You could also convert the list, remove the item at a specific index, then convert it back to an array.