vb.net remove first element from array

拜拜、爱过 提交于 2021-02-18 12:54:13

问题


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


回答1:


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




回答2:


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.




回答3:


And don't forget - now that you're using .Net, you're no longer limited to strictly using "arrays". You can (and arguably should, as appropriate) use lists, maps, queues and all kinds of other data structures goodness:

  • http://msdn.microsoft.com/en-us/library/aa289148%28v=vs.71%29.aspx

  • http://msdn.microsoft.com/en-us/library/a1y8b3b3%28v=vs.80%29.aspx

  • http://vb.net-informations.com/collections/vb.net_collections_tutorials.htm




回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/7169259/vb-net-remove-first-element-from-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!