Queue<T> 생성자
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
Queue<T> 클래스의 새 인스턴스를 초기화합니다.
오버로드
| Name | Description |
|---|---|
| Queue<T>() |
비어 있고 기본 초기 용량이 Queue<T> 있는 클래스의 새 인스턴스를 초기화합니다. |
| Queue<T>(IEnumerable<T>) |
지정된 컬렉션에서 복사된 요소를 포함하고 복사된 요소 수를 Queue<T> 수용할 수 있는 충분한 용량이 있는 클래스의 새 인스턴스를 초기화합니다. |
| Queue<T>(Int32) |
비어 있고 지정된 초기 용량을 Queue<T> 가진 클래스의 새 인스턴스를 초기화합니다. |
예제
다음 코드 예제에서는 이 생성자와 제네릭 클래스의 다른 여러 메서드를 Queue<T> 보여 줍니다. 이 코드 예제에서는 기본 용량이 있는 문자열 큐를 만들고 메서드를 Enqueue 사용하여 5개의 문자열을 큐에 대기합니다. 큐의 요소는 열거되며 큐의 상태는 변경되지 않습니다. 메서드 Dequeue 는 첫 번째 문자열을 큐에서 제거하는 데 사용됩니다. 이 Peek 메서드는 큐에서 다음 항목을 보는 데 사용된 다음 Dequeue 메서드를 사용하여 큐에서 제거합니다.
이 ToArray 메서드는 배열을 만들고 큐 요소를 복사하는 데 사용되며, 배열은 큐의 복사본을 만드는 데 사용되는 Queue<T>생성자에 전달됩니다IEnumerable<T>. 복사본의 요소가 표시됩니다.
큐 크기의 두 배인 배열이 만들어지고 CopyTo , 이 메서드는 배열의 중간에서 시작하는 배열 요소를 복사하는 데 사용됩니다. Queue<T> 생성자는 처음에 세 개의 null 요소를 포함하는 큐의 두 번째 복사본을 만드는 데 다시 사용됩니다.
이 Contains 메서드는 문자열 "4"가 큐의 첫 번째 복사본에 있음을 표시하는 데 사용되며 Clear , 그 후에 메서드는 복사본을 지우고 Count 속성은 큐가 비어 있음을 표시합니다.
using System;
using System.Collections.Generic;
class Example
{
public static void Main()
{
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
// A queue can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
}
Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}",
numbers.Peek());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
Console.WriteLine("\nContents of the first copy:");
foreach( string number in queueCopy )
{
Console.WriteLine(number);
}
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
Queue<string> queueCopy2 = new Queue<string>(array2);
Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in queueCopy2 )
{
Console.WriteLine(number);
}
Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}",
queueCopy.Contains("four"));
Console.WriteLine("\nqueueCopy.Clear()");
queueCopy.Clear();
Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
}
}
/* This code example produces the following output:
one
two
three
four
five
Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'
Contents of the first copy:
three
four
five
Contents of the second copy, with duplicates and nulls:
three
four
five
queueCopy.Contains("four") = True
queueCopy.Clear()
queueCopy.Count = 0
*/
open System
open System.Collections.Generic
let numbers = Queue()
numbers.Enqueue "one"
numbers.Enqueue "two"
numbers.Enqueue "three"
numbers.Enqueue "four"
numbers.Enqueue "five"
// A queue can be enumerated without disturbing its contents.
for number in numbers do
printfn $"{number}"
printfn $"\nDequeuing '{numbers.Dequeue()}'"
printfn $"Peek at next item to dequeue: {numbers.Peek()}"
printfn $"Dequeuing '{numbers.Dequeue()}'"
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
let queueCopy = numbers.ToArray() |> Queue
printfn $"\nContents of the first copy:"
for number in queueCopy do
printfn $"{number}"
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
let array2 = numbers.Count * 2 |> Array.zeroCreate
numbers.CopyTo(array2, numbers.Count)
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
let queueCopy2 = Queue array2
printfn $"\nContents of the second copy, with duplicates and nulls:"
for number in queueCopy2 do
printfn $"{number}"
printfn $"""\nqueueCopy.Contains "four" = {queueCopy.Contains "four"}"""
printfn $"\nqueueCopy.Clear()"
queueCopy.Clear()
printfn $"queueCopy.Count = {queueCopy.Count}"
// This code example produces the following output:
// one
// two
// three
// four
// five
//
// Dequeuing 'one'
// Peek at next item to dequeue: two
// Dequeuing 'two'
//
// Contents of the first copy:
// three
// four
// five
//
// Contents of the second copy, with duplicates and nulls:
//
//
//
// three
// four
// five
//
// queueCopy.Contains "four" = True
//
// queueCopy.Clear()
//
// queueCopy.Count = 0
Imports System.Collections.Generic
Module Example
Sub Main
Dim numbers As New Queue(Of String)
numbers.Enqueue("one")
numbers.Enqueue("two")
numbers.Enqueue("three")
numbers.Enqueue("four")
numbers.Enqueue("five")
' A queue can be enumerated without disturbing its contents.
For Each number As String In numbers
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "Dequeuing '{0}'", numbers.Dequeue())
Console.WriteLine("Peek at next item to dequeue: {0}", _
numbers.Peek())
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue())
' Create a copy of the queue, using the ToArray method and the
' constructor that accepts an IEnumerable(Of T).
Dim queueCopy As New Queue(Of String)(numbers.ToArray())
Console.WriteLine(vbLf & "Contents of the first copy:")
For Each number As String In queueCopy
Console.WriteLine(number)
Next
' Create an array twice the size of the queue, compensating
' for the fact that Visual Basic allocates an extra array
' element. Copy the elements of the queue, starting at the
' middle of the array.
Dim array2((numbers.Count * 2) - 1) As String
numbers.CopyTo(array2, numbers.Count)
' Create a second queue, using the constructor that accepts an
' IEnumerable(Of T).
Dim queueCopy2 As New Queue(Of String)(array2)
Console.WriteLine(vbLf & _
"Contents of the second copy, with duplicates and nulls:")
For Each number As String In queueCopy2
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "queueCopy.Contains(""four"") = {0}", _
queueCopy.Contains("four"))
Console.WriteLine(vbLf & "queueCopy.Clear()")
queueCopy.Clear()
Console.WriteLine(vbLf & "queueCopy.Count = {0}", _
queueCopy.Count)
End Sub
End Module
' This code example produces the following output:
'
'one
'two
'three
'four
'five
'
'Dequeuing 'one'
'Peek at next item to dequeue: two
'
'Dequeuing 'two'
'
'Contents of the copy:
'three
'four
'five
'
'Contents of the second copy, with duplicates and nulls:
'
'
'
'three
'four
'five
'
'queueCopy.Contains("four") = True
'
'queueCopy.Clear()
'
'queueCopy.Count = 0
Queue<T>()
- Source:
- Queue.cs
- Source:
- Queue.cs
- Source:
- Queue.cs
- Source:
- Queue.cs
- Source:
- Queue.cs
비어 있고 기본 초기 용량이 Queue<T> 있는 클래스의 새 인스턴스를 초기화합니다.
public:
Queue();
public Queue();
Public Sub New ()
설명
용량 Queue<T> 은 보유할 수 있는 Queue<T> 요소의 수입니다. 요소가 추가 Queue<T>되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.
컬렉션의 크기를 예측할 수 있는 경우 초기 용량을 지정하면 요소를 Queue<T>추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.
를 호출 TrimExcess하여 용량을 줄일 수 있습니다.
이 생성자는 O(1) 작업입니다.
적용 대상
Queue<T>(IEnumerable<T>)
- Source:
- Queue.cs
- Source:
- Queue.cs
- Source:
- Queue.cs
- Source:
- Queue.cs
- Source:
- Queue.cs
지정된 컬렉션에서 복사된 요소를 포함하고 복사된 요소 수를 Queue<T> 수용할 수 있는 충분한 용량이 있는 클래스의 새 인스턴스를 초기화합니다.
public:
Queue(System::Collections::Generic::IEnumerable<T> ^ collection);
public Queue(System.Collections.Generic.IEnumerable<T> collection);
new System.Collections.Generic.Queue<'T> : seq<'T> -> System.Collections.Generic.Queue<'T>
Public Sub New (collection As IEnumerable(Of T))
매개 변수
- collection
- IEnumerable<T>
요소가 새 Queue<T>요소에 복사되는 컬렉션입니다.
예외
collection은 null입니다.
설명
용량 Queue<T> 은 보유할 수 있는 Queue<T> 요소의 수입니다. 요소가 추가 Queue<T>되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.
컬렉션의 크기를 예측할 수 있는 경우 초기 용량을 지정하면 요소를 Queue<T>추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.
를 호출 TrimExcess하여 용량을 줄일 수 있습니다.
요소는 컬렉션에서 Queue<T> 읽는 순서와 동일한 순서로 IEnumerator<T> 복사됩니다.
이 생성자는 O(n) 연산입니다. 여기서 n 는 요소 수가 있습니다 collection.
적용 대상
Queue<T>(Int32)
- Source:
- Queue.cs
- Source:
- Queue.cs
- Source:
- Queue.cs
- Source:
- Queue.cs
- Source:
- Queue.cs
비어 있고 지정된 초기 용량을 Queue<T> 가진 클래스의 새 인스턴스를 초기화합니다.
public:
Queue(int capacity);
public Queue(int capacity);
new System.Collections.Generic.Queue<'T> : int -> System.Collections.Generic.Queue<'T>
Public Sub New (capacity As Integer)
매개 변수
예외
capacity가 0보다 작습니다.
설명
용량 Queue<T> 은 보유할 수 있는 Queue<T> 요소의 수입니다. 요소가 추가 Queue<T>되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.
컬렉션의 크기를 예측할 수 있는 경우 초기 용량을 지정하면 요소를 Queue<T>추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.
를 호출 TrimExcess하여 용량을 줄일 수 있습니다.
이 생성자는 O(n) 작업입니다. 여기서 n 는 다음과 같습니다 capacity.
주의
사용자 입력에서 제공되는 경우 capacity 매개 변수가 없는 생성자를 사용하고 요소가 추가되면 컬렉션의 크기를 조정하는 것이 좋습니다. 사용자 지정 값을 사용해야 하는 경우 적절한 제한(예 Math.Clamp(untrustedValue, 0, 20): )으로 고정하거나 요소 수가 지정된 값과 일치하는지 확인합니다.