如何从 VB.NET 中的数组中删除项目? [英] How can I delete an item from an array in VB.NET?

查看:90
本文介绍了如何从 VB.NET 中的数组中删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 VB.NET 中的数组中删除项目?>

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

推荐答案

正如 Heinzi 所说,数组具有固定大小.为了删除项目"或调整大小",您必须创建一个具有所需大小的新数组,并根据需要复制您需要的项目.

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

你可以这样使用它:

Module Module1

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

上面的代码从数组中删除了第二个元素(mno")[其索引为 1].

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

您需要在 .NET 3.5 或更高版本中进行开发才能使用扩展方法.如果您使用的是 .NET 2.0 或 3.0,则可以这样调用该方法

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)

我希望这是你需要的.

基于 ToolMakerSteve 的评论 看来初始代码没有修改您要更新的数组,因为函数声明中使用了 ByVal.但是,编写像 arr = arr.RemoveAt(1)arr = RemoveAt(arr, 1) 这样的代码确实会修改数组,因为它将修改后的数组重新分配给原始数组.

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

注意:

  1. 我在该过程中使用了一个临时数组,因为它使我的意图更加明确,而这正是您执行 Redim Preserve 时 VB.NET 在幕后所做的.如果您想使用 Redim Preserve 就地修改数组,请参阅 ToolmakerSteve 的回答.

  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.

此处编写的 RemoveAt 方法是扩展方法.为了让它们工作,您必须将它们粘贴到 Module 中.如果将扩展方法放在 Class 中,它们将无法在 VB.NET 中工作.

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.

重要如果您要修改数组并进行大量删除",强烈建议使用不同的数据结构,例如 List(Of T)正如其他回答者对该问题的建议.

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.

这篇关于如何从 VB.NET 中的数组中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆