Queue: ToArray

ToArray

Copies the Queue elements to a new array.



 Public Function ToArray ( ) As Variant ( )

Return Values

Variant() -  A new array containing elements copied from the Queue.

Remarks

The Queue is not modified. The order of the elements in the new array is the same as the order of the elements from the beginning of the Queue to its end.

This method is an O(n) operation, where n is Count.

Examples

The following example shows how to copy a Queue into a one-dimensional array.

Public Sub Main()
    Dim MySourceQ As New Queue
    Dim MyTargetArray() As String
    Dim MyStandardArray() As Variant
    
    ' Initializes the source Queue.
    MySourceQ.Enqueue "three"
    MySourceQ.Enqueue "napping"
    MySourceQ.Enqueue "cats"
    MySourceQ.Enqueue "in"
    MySourceQ.Enqueue "the"
    MySourceQ.Enqueue "barn"

    MyTargetArray = NewStrings("The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog")
    ReDim Preserve MyTargetArray(0 To 14)
    
    ' Displays the values of the target array.
    Debug.Print "The target Array contains the following (before and after copying):"
    PrintValues MyTargetArray
    
    ' Copies the entire source queue to the target array, starting at index 6.
    MySourceQ.CopyTo MyTargetArray, 6
    
    ' Displays the values of the target array.
    PrintValues MyTargetArray
    
    ' Copies the entire source queue to a new standard array.
    MyStandardArray = MySourceQ.ToArray
    
    ' Displays the values of the new standard array.
    Debug.Print "The new standard array contains the following:"
    PrintValues MyStandardArray
End Sub

Private Sub PrintValues(ByRef MyArr As Variant)
    Dim Item As Variant
    
    For Each Item In MyArr
        Debug.Print " " & Item;
    Next
    
    Debug.Print
End Sub

' This example code produces the following output.
'
'    The target Array contains the following (before and after copying):
'     The quick brown fox jumped over the lazy dog
'     The quick brown fox jumped over three napping cats in the barn
'    The new standard array contains the following:
'     three napping cats in the barn

See Also

Project CorLib Overview

Class Queue Overview

CopyTo