How can I delete an item from an array in VB.NET?

前端 未结 11 1684
别那么骄傲
别那么骄傲 2020-12-05 18:14

How can I delete an item from an array in VB.NET?

相关标签:
11条回答
  • 2020-12-05 18:19

    This may be a lazy man's solution, but can't you just delete the contents of the index you want removed by reassigning their values to 0 or "" and then ignore/skip these empty array elements instead of recreating and copying arrays on and off?

    0 讨论(0)
  • 2020-12-05 18:21

    One line using LINQ:

    Dim arr() As String = {"uno", "dos", "tres", "cuatro", "cinco"}
    Dim indx As Integer = 2
    arr = arr.Where(Function(item, index) index <> indx).ToArray 'arr = {"uno", "dos", "cuatro", "cinco"}
    

    Remove first element:

    arr = arr.Skip(1).ToArray
    

    Remove last element:

    arr = arr.Take(arr.length - 1).ToArray
    
    0 讨论(0)
  • 2020-12-05 18:21

    That depends on what you mean by delete. An array has a fixed size, so deleting doesn't really make sense.

    If you want to remove element i, one option would be to move all elements j > i one position to the left (a[j - 1] = a[j] for all j, or using Array.Copy) and then resize the array using ReDim Preserve.

    So, unless you are forced to use an array by some external constraint, consider using a data structure more suitable for adding and removing items. List<T>, for example, also uses an array internally but takes care of all the resizing issues itself: For removing items, it uses the algorithm mentioned above (without the ReDim), which is why List<T>.RemoveAt is an O(n) operation.

    There's a whole lot of different collection classes in the System.Collections.Generic namespace, optimized for different use cases. If removing items frequently is a requirement, there are lots of better options than an array (or even List<T>).

    0 讨论(0)
  • 2020-12-05 18:22

    The variable i represents the index of the element you want to delete:

    System.Array.Clear(ArrayName, i, 1)
    
    0 讨论(0)
  • Seems like this sounds more complicated than it is...

        Dim myArray As String() = TextBox1.Lines
        'First we count how many null elements there are...
        Dim Counter As Integer = 0
        For x = 0 To myArray.Count - 1
            If Len(myArray(x)) < 1 Then
                Counter += 1
            End If
        Next
        'Then we dimension an array to be the size of the last array
        'minus the amount of nulls found...
        Dim tempArr(myArray.Count - Counter) As String
    
        'Indexing starts at zero, so let's set the stage for that...
        Counter = -1
    
        For x = 0 To myArray.Count - 1
            'Set the conditions for the new array as in
            'It .contains("word"), has no value, length is less than 1, ect.
            If Len(myArray(x)) > 1 Then
                Counter += 1
                'So if a value is present, we move that value over to
                'the new array.
                tempArr(Counter) = myArray(x)
            End If
        Next
    

    Now you can assign tempArr back to the original or what ever you need done with it as in...

    TextBox1.Lines = tempArr (You now have a textbox void of blank lines)

    0 讨论(0)
  • 2020-12-05 18:27

    As Heinzi said, an array has a fixed size. In order to 'remove an item' or 'resize' it, you'll have to create a new array with the desired size and copy the items you need as appropriate.

    Here's code to remove an item from an array:

    <System.Runtime.CompilerServices.Extension()> _
    Function RemoveAt(Of T)(ByVal arr As T(), ByVal index As Integer) As T()
        Dim uBound = arr.GetUpperBound(0)
        Dim lBound = arr.GetLowerBound(0)
        Dim arrLen = uBound - lBound
    
        If index < lBound OrElse index > uBound Then
            Throw New ArgumentOutOfRangeException( _
            String.Format("Index must be from {0} to {1}.", lBound, uBound))
    
        Else
            'create an array 1 element less than the input array
            Dim outArr(arrLen - 1) As T
            'copy the first part of the input array
            Array.Copy(arr, 0, outArr, 0, index)
            'then copy the second part of the input array
            Array.Copy(arr, index + 1, outArr, index, uBound - index)
    
            Return outArr
        End If
    End Function
    

    You can use it as such:

    Module Module1
    
        Sub Main()
            Dim arr = New String() {"abc", "mno", "xyz"}
            arr.RemoveAt(1)
        End Sub
    End Module
    

    The code above removes the second element ("mno") [which has an index of 1] from the array.

    You need to be developing in .NET 3.5 or higher in order to use the extension method. If you're using .NET 2.0 or 3.0, you can call the method as such

    arr = RemoveAt(arr, 1)
    

    I hope this is what you need.

    Update

    After running tests based on ToolMakerSteve's comment it appears the initial code does not modify the array you want to update because of the ByVal used in the function's declaration. However, writing code like arr = arr.RemoveAt(1) or arr = RemoveAt(arr, 1) does modify the array because it reassigns the modified array to the original.

    Find below the updated method (subroutine) for removing an element from an array.

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub RemoveAt(Of T)(ByRef arr As T(), ByVal index As Integer)
        Dim uBound = arr.GetUpperBound(0)
        Dim lBound = arr.GetLowerBound(0)
        Dim arrLen = uBound - lBound
    
        If index < lBound OrElse index > uBound Then
            Throw New ArgumentOutOfRangeException( _
            String.Format("Index must be from {0} to {1}.", lBound, uBound))
    
        Else
            'create an array 1 element less than the input array
            Dim outArr(arrLen - 1) As T
            'copy the first part of the input array
            Array.Copy(arr, 0, outArr, 0, index)
            'then copy the second part of the input array
            Array.Copy(arr, index + 1, outArr, index, uBound - index)
    
            arr = outArr
        End If
    End Sub
    

    Usage of the method is similar to the original, except there is no return value this time so trying to assign an array from the return value will not work because nothing is returned.

    Dim arr = New String() {"abc", "mno", "xyz"}
    arr.RemoveAt(1)  ' Output: {"abc", "mno"} (works on .NET 3.5 and higher)
    RemoveAt(arr, 1) ' Output: {"abc", "mno"} (works on all versions of .NET fx)
    arr = arr.RemoveAt(1)  'will not work; no return value
    arr = RemoveAt(arr, 1) 'will not work; no return value
    

    Note:

    1. I use a temporary array for the process because it makes my intentions clear and that is exactly what VB.NET does behind the scenes when you do Redim Preserve. If you would like to modify the array in-place using Redim Preserve, see ToolmakerSteve's answer.

    2. The RemoveAt methods written here are extension methods. In order for them to work, you will have to paste them in a Module. Extension methods will not work in VB.NET if they are placed in a Class.

    3. Important If you will be modifying your array with lots of 'removes', it is highly recommended to use a different data structure such as List(Of T) as suggested by other answerers to this question.

    0 讨论(0)
提交回复
热议问题