當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET ArrayList.InsertRange方法代碼示例

本文整理匯總了VB.NET中System.Collections.ArrayList.InsertRange方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET ArrayList.InsertRange方法的具體用法?VB.NET ArrayList.InsertRange怎麽用?VB.NET ArrayList.InsertRange使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Collections.ArrayList的用法示例。


在下文中一共展示了ArrayList.InsertRange方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Main

' 導入命名空間
Imports System.Collections

Public Class SamplesArrayList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new ArrayList using Insert instead of Add.
        Dim myAL As New ArrayList()
        myAL.Insert(0, "The")
        myAL.Insert(1, "fox")
        myAL.Insert(2, "jumps")
        myAL.Insert(3, "over")
        myAL.Insert(4, "the")
        myAL.Insert(5, "dog")
        
        ' Creates and initializes a new Queue.
        Dim myQueue As New Queue()
        myQueue.Enqueue("quick")
        myQueue.Enqueue("brown")
        
        ' Displays the ArrayList and the Queue.
        Console.WriteLine("The ArrayList initially contains the following:")
        PrintValues(myAL)
        Console.WriteLine("The Queue initially contains the following:")
        PrintValues(myQueue)
        
        ' Copies the Queue elements to the ArrayList at index 1.
        myAL.InsertRange(1, myQueue)
        
        ' Displays the ArrayList.
        Console.WriteLine("After adding the Queue, the ArrayList now contains:")
        PrintValues(myAL)
        
        ' Search for "dog" and add "lazy" before it.
        myAL.Insert(myAL.IndexOf("dog"), "lazy")
        
        ' Displays the ArrayList.
        Console.WriteLine("After adding ""lazy"", the ArrayList now contains:")
        PrintValues(myAL)
        
        ' Add "!!!" at the end.
        myAL.Insert(myAL.Count, "!!!")
        
        ' Displays the ArrayList.
        Console.WriteLine("After adding ""!!!"", the ArrayList now contains:")
        PrintValues(myAL)
        
        ' Inserting an element beyond Count throws an exception.
        Try
            myAL.Insert(myAL.Count + 1, "anystring")
        Catch myException As Exception
            Console.WriteLine("Exception: " + myException.ToString())
        End Try
    End Sub    
    
    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.Write("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class
開發者ID:VB.NET開發者,項目名稱:System.Collections,代碼行數:65,代碼來源:ArrayList.InsertRange

輸出:

The ArrayList initially contains the following:
The    fox    jumps    over    the    dog
The Queue initially contains the following:
quick    brown
After adding the Queue, the ArrayList now contains:
The    quick    brown    fox    jumps    over    the    dog
After adding "lazy", the ArrayList now contains:
The    quick    brown    fox    jumps    over    the    lazy    dog
After adding "!!!", the ArrayList now contains:
The    quick    brown    fox    jumps    over    the    lazy    dog    !!!
Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
Parameter name: index
at System.Collections.ArrayList.Insert(Int32 index, Object value)
at SamplesArrayList.Main()


注:本文中的System.Collections.ArrayList.InsertRange方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。