×

VB.Net Programs

VB.Net program to copy the queue elements into an array using the collection

By Nidhi Last Updated : November 16, 2024

Copy the queue elements into an array in VB.Net

Here, we will use the CopyTo() method of Queue collection to copy the elements of queue collection into a string array and print on the console screen.

Program/Source Code:

The source code to copy the queue elements into the array using collection is given below. The given program is compiled and executed successfully.

VB.Net code to copy the queue elements into an array

'VB.Net program to copy the queue elements into the array 
'using the collection. 

Imports System
Imports System.Collections

Module Module1
    Sub Main()

        Dim arr(4) As String
        Dim queue As New Queue(5)

        queue.Enqueue("India")
        queue.Enqueue("USA")
        queue.Enqueue("UK")
        queue.Enqueue("CHINA")

        queue.CopyTo(arr, 0)

        Console.WriteLine("Array elements are:")
        For i = 0 To 3 Step 1
            Console.WriteLine(vbTab & "Item[" & (i + 1) & "]: " & arr(i))
        Next
    End Sub
End Module

Output

Array elements are:
        Item[1]: India
        Item[2]: USA
        Item[3]: UK
        Item[4]: CHINA
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program, And, we created an object of the Queue collection class for 5 elements. After that, we inserted 4 items using the Enqueue() method and then copied the items of queue collection into the "arr" array and then printed the elements of the array on the console screen.

VB.Net Data Structure Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.