Export (0) Print
Expand All

Queue.ToArray Method

Copies the Queue elements to a new array.

[Visual Basic]
Public Overridable Function ToArray() As Object()
[C#]
public virtual object[] ToArray();
[C++]
public: virtual Object* ToArray()  __gc[];
[JScript]
public function ToArray() : Object[];

Return Value

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.

Example

[Visual Basic, C#, C++] The following example shows how to copy a Queue into a one-dimensional Array instance and into a one-dimensional standard array.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesQueue    
    
    Public Shared Sub Main()
        
        ' Creates and initializes the source Queue.
        Dim mySourceQ As New Queue()
        mySourceQ.Enqueue("three")
        mySourceQ.Enqueue("napping")
        mySourceQ.Enqueue("cats")
        mySourceQ.Enqueue("in")
        mySourceQ.Enqueue("the")
        mySourceQ.Enqueue("barn")
        
        ' Creates and initializes the one-dimensional target Array.
        Dim myTargetArray As Array = Array.CreateInstance(GetType(String), 15)
        myTargetArray.SetValue("The", 0)
        myTargetArray.SetValue("quick", 1)
        myTargetArray.SetValue("brown", 2)
        myTargetArray.SetValue("fox", 3)
        myTargetArray.SetValue("jumped", 4)
        myTargetArray.SetValue("over", 5)
        myTargetArray.SetValue("the", 6)
        myTargetArray.SetValue("lazy", 7)
        myTargetArray.SetValue("dog", 8)
        
        ' Displays the values of the target Array.
        Console.WriteLine("The target Array contains the " & _
           "following (before and after copying):")
        PrintValues(myTargetArray, " "c)
        
        ' 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, " "c)
        
        ' Copies the entire source Queue to a new standard array.
        Dim myStandardArray As Object() = mySourceQ.ToArray()
        
        ' Displays the values of the new standard array.
        Console.WriteLine("The new standard array contains the following:")
        PrintValues(myStandardArray, " "c)
    End Sub
    
    Overloads Public Shared Sub PrintValues(myArr As Array, _
       mySeparator As Char)
       
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub    
    
    Overloads Public Shared Sub PrintValues(myArr() As Object, _
       mySeparator As Char)
       
        Dim myObj As Object
        For Each myObj In  myArr
            Console.Write("{0}{1}", mySeparator, myObj)
        Next myObj
        Console.WriteLine()
    End Sub
End Class

' This 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 

[C#] 
using System;
using System.Collections;
public class SamplesQueue  {

   public static void Main()  {

      // Creates and initializes the source Queue.
      Queue mySourceQ = new Queue();
      mySourceQ.Enqueue( "three" );
      mySourceQ.Enqueue( "napping" );
      mySourceQ.Enqueue( "cats" );
      mySourceQ.Enqueue( "in" );
      mySourceQ.Enqueue( "the" );
      mySourceQ.Enqueue( "barn" );

      // Creates and initializes the one-dimensional target Array.
      Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
      myTargetArray.SetValue( "The", 0 );
      myTargetArray.SetValue( "quick", 1 );
      myTargetArray.SetValue( "brown", 2 );
      myTargetArray.SetValue( "fox", 3 );
      myTargetArray.SetValue( "jumped", 4 );
      myTargetArray.SetValue( "over", 5 );
      myTargetArray.SetValue( "the", 6 );
      myTargetArray.SetValue( "lazy", 7 );
      myTargetArray.SetValue( "dog", 8 );

      // Displays the values of the target Array.
      Console.WriteLine( "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.
      Object[] myStandardArray = mySourceQ.ToArray();

      // Displays the values of the new standard array.
      Console.WriteLine( "The new standard array contains the following:" );
      PrintValues( myStandardArray, ' ' );
   }


   public static void PrintValues( Array myArr, char mySeparator )  {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength( myArr.Rank - 1 );
      while ( myEnumerator.MoveNext() )  {
         if ( i < cols )  {
            i++;
         } else  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
      }
      Console.WriteLine();
   }

   public static void PrintValues( Object[] myArr, char mySeparator )  {
      foreach ( Object myObj in myArr )  {
         Console.Write( "{0}{1}", mySeparator, myObj );
      }
      Console.WriteLine();
   }
}
/* 
This 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
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void PrintValues( Array* myArr, Char mySeparator )  {
   System::Collections::IEnumerator* myEnumerator = myArr->GetEnumerator();
   int i = 0;
   int cols = myArr->GetLength( myArr->Rank - 1 );
   while ( myEnumerator->MoveNext() )  {
      if ( i < cols )  {
         i++;
      } else  {
         Console::WriteLine();
         i = 1;
      }
      Console::Write( S"{0}{1}", __box(mySeparator), myEnumerator->Current );
   }
   Console::WriteLine();
}

void PrintValues( Object* myArr __gc[], Char mySeparator )  {
   for( int i(0); i < myArr->Count; ++i ) {
      Console::Write( S"{0}{1}", __box(mySeparator), myArr->Item[i] );
   }
   Console::WriteLine();
}

int main()  {

   // Creates and initializes the source Queue.
   Queue* mySourceQ = new Queue();
   mySourceQ->Enqueue( S"three" );
   mySourceQ->Enqueue( S"napping" );
   mySourceQ->Enqueue( S"cats" );
   mySourceQ->Enqueue( S"in" );
   mySourceQ->Enqueue( S"the" );
   mySourceQ->Enqueue( S"barn" );

   // Creates and initializes the one-dimensional target Array.
   Array* myTargetArray=Array::CreateInstance( __typeof(String), 15 );
   myTargetArray->SetValue( S"The", 0 );
   myTargetArray->SetValue( S"quick", 1 );
   myTargetArray->SetValue( S"brown", 2 );
   myTargetArray->SetValue( S"fox", 3 );
   myTargetArray->SetValue( S"jumped", 4 );
   myTargetArray->SetValue( S"over", 5 );
   myTargetArray->SetValue( S"the", 6 );
   myTargetArray->SetValue( S"lazy", 7 );
   myTargetArray->SetValue( S"dog", 8 );

   // Displays the values of the target Array.
   Console::WriteLine( S"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.
   Object* myStandardArray __gc[] = mySourceQ->ToArray();

   // Displays the values of the new standard array.
   Console::WriteLine( S"The new standard array contains the following:" );
   PrintValues( myStandardArray, ' ' );
}

/*
This 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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Queue Class | Queue Members | System.Collections Namespace

Show:
© 2014 Microsoft