获取VB中指定索引范围的子数组

huangapple go评论113阅读模式
英文:

get a subarray with a range of indexes in VB

问题

在其他语言中,如果我有一个数组,我可以使用this_array[starting_index..ending_index]来获取数组的切片。

VB 中是否有类似的功能?我在文档中没有找到类似的内容。

英文:

In other languages if I have an array I can just take a slice of this array with this_array[starting_index..ending_index].

Is there something similar in VB? I didn't find anything like that in the documentation.

答案1

得分: 1

以下是已翻译的内容:

有几种方法可以处理这个问题。这些方法都不直接涉及System.Array,这可能是为什么你在文档中找不到它们的原因。

一种选择是使用ArraySegment(Of T)。其中一个构造函数接受源数组、第一个索引和项目数量。在你的情况下,你可以这样写:

Dim myArray As Integer() = '... 无论什么

'...

Dim segment = New ArraySegment(Of Integer)(myArray, 
                                           starting_index, ending_index - starting_index + 1)

ArraySegment(Of T) 实现了ICollection(Of T)IList(Of T),但我认为这实际上是一个糟糕的主意(违反了LSP),因为它在修改操作上会引发NotSupportedException

另一种选择是使用 Linq,它将给你一个IEnumerable(Of T)(可以使用ToArray将其转换为数组,或使用ToList将其转换为List(Of T)):

Dim myArray As Integer() = '... 无论什么

'...

Dim segment = myArray.Skip(starting_index).Take(ending_index - starting_index + 1)

如果starting_index是数组下界(通常为0),则可以省略Skip

还有一种选择是使用Array.Copy来完成。这需要显式分配目标数组。

Dim myArray As Integer() = '... 无论什么

'...

Dim segment As Integer()
ReDim segment(0 To ending_index - starting_index)
Array.Copy(myArray, starting_index, segment, 0, ending_index - starting_index + 1)
英文:

There are a couple of ways you can approach this. Neither of them is directly on System.Array, which may be why you didn't find them in the docs.

One option is ArraySegment(Of T). One of its constructors takes the source array, the first index, and the count of items. In your case, you would write:

Dim myArray As Integer() = '... whatever

'...

Dim segment = New ArraySegment(Of Integer)(myArray, 
                                           starting_index, ending_index - starting_index + 1)

ArraySegment(Of T) implements ICollection(Of T) and IList(Of T), which I would argue is actually a terrible idea (a violation of LSP) because it throws NotSupportedException on the modifying operations.

Another option is to use Linq, which will give you an IEnumerable(Of T) (which can be converted into an array using ToArray or a List(Of T) using ToList):

Dim myArray As Integer() = '... whatever

'...

Dim segment = myArray.Skip(starting_index).Take(ending_index - starting_index + 1)

The Skip can be elided if starting_index is the array lower bound (which is usually 0).

Still another option would be to use Array.Copy to do it. This requires explicitly allocating the destination array.

Dim myArray As Integer() = '... whatever

'...

Dim segment As Integer()
ReDim segment(0 To ending_index - starting_index)
Array.Copy(myArray, starting_index, segment, 0, ending_index - starting_index + 1)

huangapple
  • 本文由 发表于 2023年4月11日 03:42:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定