vb.net 从数组中删除第一个元素

发布于 2024-12-01 02:49:37 字数 45 浏览 2 评论 0原文

一种答案是创建一个短一个元素的新数组。还有其他更简单的方法可以做到这一点吗?

One answer is to create a new array that is one element shorter. Are there any other simpler ways to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

束缚m 2024-12-08 02:49:37

您可以使用 LINQ 以非常简洁的代码生成结果:

Dim a2 = a.Skip(1).ToArray();

您可能会有批评者说这很慢,您应该使用 Array.Copy 代替:

Dim a2(a.Length - 2) as Integer 
Array.Copy(a, 1, a2, 0, a.Length - 1)

但是,我测试了两者的时间使用具有 1,000,000 个元素的整数数组的方法,发现 LINQ 花费了 29 毫秒,直接复制花费了 3 毫秒。除非您正在使用大量元素进行某种疯狂的数学运算,否则 LINQ 就很好,而且可读性更高。

You can use LINQ to produce your result in a very concise bit of code:

Dim a2 = a.Skip(1).ToArray();

You may have detractors say that this is slow and that you should use Array.Copy instead:

Dim a2(a.Length - 2) as Integer 
Array.Copy(a, 1, a2, 0, a.Length - 1)

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.

是伱的 2024-12-08 02:49:37

这是在 vb.net 中删除数组第一个元素的一种方法。

dim a(n)
...
for i = 1 to ubound(a)
  a(i-1) = a(i)
  next i
redim preserve a(ubound(a)-1)

您可以为此创建一个函数来删除数组的任意元素(有一个用于 for 循环的初始值的参数)。

Here is one way to remove the first element of an array in vb.net.

dim a(n)
...
for i = 1 to ubound(a)
  a(i-1) = a(i)
  next i
redim preserve a(ubound(a)-1)

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).

冧九 2024-12-08 02:49:37

结合 @xpda 和 @Enigmativity 的答案,观察到 Array.Copy 可以安全地用于复制回原始数组。引用 Array.Copy 方法的 msdn 页面:

如果sourceArray 和destinationArray 重叠,则此方法的行为就像在覆盖destinationArray 之前将sourceArray 的原始值保留在临时位置中一样。

下面是一个(扩展)子例程,它将删除任意类型数组中指定索引处的元素:

' Remove element at index "index". Result is one element shorter.
' Similar to List.RemoveAt, but for arrays.
<System.Runtime.CompilerServices.Extension()> _
Public Sub RemoveAt(Of T)(ByRef a() As T, ByVal index As Integer)
    ' Move elements after "index" down 1 position.
    Array.Copy(a, index + 1, a, index, UBound(a) - index)
    ' Shorten by 1 element.
    ReDim Preserve a(UBound(a) - 1)
End Sub

用法示例(假设数组以索引 0 开头):

a.RemoveAt(0)    ' Remove first element
a.RemoveAt(1)    ' Remove second element.
a.RemoveAt(UBound(a))    ' Remove last element

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:

If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved in a temporary location before destinationArray is overwritten.

Here is an (extension) subroutine that will remove element, at specified index, of an array of any type:

' Remove element at index "index". Result is one element shorter.
' Similar to List.RemoveAt, but for arrays.
<System.Runtime.CompilerServices.Extension()> _
Public Sub RemoveAt(Of T)(ByRef a() As T, ByVal index As Integer)
    ' Move elements after "index" down 1 position.
    Array.Copy(a, index + 1, a, index, UBound(a) - index)
    ' Shorten by 1 element.
    ReDim Preserve a(UBound(a) - 1)
End Sub

Usage examples (assuming array starting with index 0):

a.RemoveAt(0)    ' Remove first element
a.RemoveAt(1)    ' Remove second element.
a.RemoveAt(UBound(a))    ' Remove last element
电影里的梦 2024-12-08 02:49:37
Public xArray as variant

Function Array_DeleteFirstItem()
    Dim i As Integer
    For i = 0 To UBound(xArray) - 1
        xArray (LBound(xArray) + i) = xArray(LBound(NotContainsArray) + i + 1)
    Next
    ReDim Preserve xArray(UBound(NotContainsArray) - 1)

    For i = 0 To UBound(xArray)
        Debug.Print xArray(i)
    Next
End Function
Public xArray as variant

Function Array_DeleteFirstItem()
    Dim i As Integer
    For i = 0 To UBound(xArray) - 1
        xArray (LBound(xArray) + i) = xArray(LBound(NotContainsArray) + i + 1)
    Next
    ReDim Preserve xArray(UBound(NotContainsArray) - 1)

    For i = 0 To UBound(xArray)
        Debug.Print xArray(i)
    Next
End Function
韵柒 2024-12-08 02:49:37

您还可以转换列表,删除特定索引处的项目,然后将其转换回数组。

 Dim tempList = strSplit.ToList
 tempList.RemoveAt(0)
 Dim newArray = tempList.ToArray

You could also convert the list, remove the item at a specific index, then convert it back to an array.

 Dim tempList = strSplit.ToList
 tempList.RemoveAt(0)
 Dim newArray = tempList.ToArray
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文