5

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

2
  • Could you please give us a code sample of what you're trying to do, and a bit more information about what's going wrong? Commented Aug 24, 2011 at 0:55
  • 1
    See stackoverflow.com/questions/3448103/… for the general solution, of removing any element of an array, not just the first one. Commented Jun 17, 2013 at 2:40

5 Answers 5

16

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.

Sign up to request clarification or add additional context in comments.

3 Comments

RE "LINQ is fine and is far more readable." What is even more readable is to define a method that drops the first element of an array. Then call that method. See DropFirstElement of my answer to stackoverflow.com/questions/3448103/… If you like the LINQ code above, the line of code to put inside DropFirstElement would be "a = a.Skip(1).ToArray()". Or to avoid modifying input array, define as "Public Function DropFirstElement(Of T)(ByVal a() As T) As T()", with contents "Return a.Skip(1).ToArray()".
@ToolmakerSteve - I'm going to belatedly disagree. Calling a method DropFirstElement isn't more readable. If the common parlance is "Skip" then "Drop" might mean something else. Also it doesn't explicitly say that it returns an array. So it might be better to call it SkipFirstElementAndReturnAsArray. That's better. But I still think that .Skip(1).ToArray() is actually more readable still and less ambiguous.
OK, I see your reasoning. "skip" makes sense when working with enumerators. FYI, "drop" is an ancient term -- I think it is from LISP. I will point out that there is no reason to say that it returns as an array: an array is what we were working with, so an array is the expected result. The only reason you need to specify "ToArray" in your LINQ version, is because Skip converts from the original array to an enumerator. In summary, if one is thinking "enumerator", then what you say is sensible. But if one is simply manipulating an "array", needing to say "ToArray" is mildly distasteful.
4

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

1 Comment

While technically correct, using a loop is an expensive way to solve this. See Enigmativity's answer instead. Or see stackoverflow.com/questions/3448103/… for answers that support removing ANY element, not just the first one.
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

Comments

0
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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.