• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

VB.NET ArraySegment<T>结构体代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了VB.NET中System.ArraySegment<T>结构体的典型用法代码示例。如果您正苦于以下问题:VB.NET ArraySegment<T>结构体的具体用法?VB.NET ArraySegment<T>怎么用?VB.NET ArraySegment<T>使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。



在下文中一共展示了ArraySegment<T>结构体的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。

示例1: SamplesArray

Public Class SamplesArray

    Public Shared Sub Main()

        ' Create and initialize a new string array.
        Dim myArr As String() =  {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}

        ' Display the initial contents of the array.
        Console.WriteLine("The original array initially contains:")
        PrintIndexAndValues(myArr)

        ' Define an array segment that contains the entire array.
        Dim myArrSegAll As New ArraySegment(Of String)(myArr)

        ' Display the contents of the ArraySegment.
        Console.WriteLine("The first array segment (with all the array's elements) contains:")
        PrintIndexAndValues(myArrSegAll)

        ' Define an array segment that contains the middle five values of the array.
        Dim myArrSegMid As New ArraySegment(Of String)(myArr, 2, 5)

        ' Display the contents of the ArraySegment.
        Console.WriteLine("The second array segment (with the middle five elements) contains:")
        PrintIndexAndValues(myArrSegMid)

        ' Modify the fourth element of the first array segment myArrSegAll.
        myArrSegAll.Array(3) = "LION"

        ' Display the contents of the second array segment myArrSegMid.
        ' Note that the value of its second element also changed.
        Console.WriteLine("After the first array segment is modified, the second array segment now contains:")
        PrintIndexAndValues(myArrSegMid)

    End Sub

    Public Shared Sub PrintIndexAndValues(arrSeg As ArraySegment(Of String))
        Dim i As Integer
        For i = arrSeg.Offset To (arrSeg.Offset + arrSeg.Count - 1)
            Console.WriteLine("   [{0}] : {1}", i, arrSeg.Array(i))
        Next i
        Console.WriteLine()
    End Sub

    Public Shared Sub PrintIndexAndValues(myArr as String())
        Dim i As Integer
        For i = 0 To (myArr.Length - 1)
            Console.WriteLine("   [{0}] : {1}", i, myArr(i))
        Next i
        Console.WriteLine()
    End Sub

End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:52,代码来源:ArraySegment

输出:

The original array initially contains:
[0] : The
[1] : quick
[2] : brown
[3] : fox
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog

The first array segment (with all the array's elements) contains:
[0] : The
[1] : quick
[2] : brown
[3] : fox
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog

The second array segment (with the middle five elements) contains:
[2] : brown
[3] : fox
[4] : jumps
[5] : over
[6] : the

After the first array segment is modified, the second array segment now contains:
[2] : brown
[3] : LION
[4] : jumps
[5] : over
[6] : the


示例2: Example

' 导入命名空间
Imports System.Collections.Generic
Imports System.Threading.Tasks

Module Example
  Private Const SegmentSize As Integer = 10
  
   Public Sub Main()
      Dim tasks As New List(Of Task)
      
       ' Create array.
      Dim arr(49) As Integer
      For ctr As Integer = 0 To arr.GetUpperBound(0)
         arr(ctr) = ctr + 1
      Next

      ' Handle array in segments of 10.
      For ctr As Integer = 1 To CInt(Math.Ceiling(arr.Length / segmentSize))
         Dim multiplier As Integer = ctr
         Dim elements As Integer = If((multiplier - 1) * 10 + segmentSize > arr.Length,
                                      arr.Length - (multiplier - 1) * 10,
                                      segmentSize)
         Dim segment As New ArraySegment(Of Integer)(arr, (ctr - 1) * 10, elements)
         tasks.Add(Task.Run( Sub()
                                Dim list As IList(Of Integer) = CType(segment, IList(Of Integer))
                                For index As Integer = 0 To list.Count - 1
                                   list(index) = list(index) * multiplier
                                Next
                             End Sub ))
      Next
      Try
         Task.WaitAll(tasks.ToArray())
         Dim elementsShown As Integer = 0
         For Each value In arr
            Console.Write("{0,3} ", value)
            elementsShown += 1
            If elementsShown Mod 18 = 0 Then Console.WriteLine()
         Next
      Catch e As AggregateException
         Console.WriteLine("Errors occurred when working with the array:")
         For Each inner As Exception In e.InnerExceptions
            Console.WriteLine("{0}: {1}", inner.GetType().Name, inner.Message)
         Next
      End Try
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:46,代码来源:ArraySegment

输出:

1   2   3   4   5   6   7   8   9  10  22  24  26  28  30  32  34  36
38  40  63  66  69  72  75  78  81  84  87  90 124 128 132 136 140 144
148 152 156 160 205 210 215 220 225 230 235 240 245 250


示例3: Example

' 导入命名空间
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim names() As String = { "Adam", "Bruce", "Charles", "Daniel", 
                                "Ebenezer", "Francis", "Gilbert", 
                                "Henry", "Irving", "John", "Karl",
                                "Lucian", "Michael" }
      Dim partNames As New ArraySegment(Of String)(names, 2, 5)
      
      ' Cast the ArraySegment object to an IList<String> and enumerate it.
      Dim list = CType(partNames, IList(Of String))
      For ctr As Integer = 0 To list.Count - 1
         Console.WriteLine(list(ctr))
      Next     
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:18,代码来源:ArraySegment

输出:

Charles
Daniel
Ebenezer
Francis
Gilbert



注:本文中的System.ArraySegment<T>结构体示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET ConsoleKeyInfo结构体代码示例发布时间:2022-05-24
下一篇:
VB.NET StateBag类代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap