Queue<T> 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
개체의 선입선출(FIFO) 컬렉션을 나타냅니다.
generic <typename T>
public ref class Queue : System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::ICollection
generic <typename T>
public ref class Queue : System::Collections::Generic::IEnumerable<T>, System::Collections::ICollection
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection
type Queue<'T> = class
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ICollection
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Queue<'T> = class
interface seq<'T>
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Queue<'T> = class
interface seq<'T>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<'T>
type Queue<'T> = class
interface seq<'T>
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Queue<'T> = class
interface seq<'T>
interface ICollection
interface IReadOnlyCollection<'T>
interface IEnumerable
Public Class Queue(Of T)
Implements ICollection, IEnumerable(Of T), IReadOnlyCollection(Of T)
Public Class Queue(Of T)
Implements ICollection, IEnumerable(Of T)
형식 매개 변수
- T
큐에 있는 요소의 형식을 지정합니다.
- 상속
-
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> 저장된 개체는 한쪽 끝에 삽입되고 다른 쪽 끝에서 제거됩니다. 큐 및 스택은 정보에 대한 임시 스토리지가 필요할 때 유용합니다. 즉, 값을 검색한 후 요소를 삭제할 수 있습니다. 컬렉션에 저장된 것과 동일한 순서로 정보에 액세스해야 하는 경우 를 사용합니다 Queue<T> . 정보에 역순으로 액세스해야 하는 경우 를 사용합니다 Stack<T> . 여러 스레드에서 동시에 컬렉션에 액세스해야 하는 경우 또는 ConcurrentStack<T> 를 사용합니다ConcurrentQueue<T>.
및 해당 요소에 대해 Queue<T> 세 가지 기본 작업을 수행할 수 있습니다.
의 Queue<T> 용량은 가 보유할 수 있는 Queue<T> 요소의 수입니다. 요소가 에 Queue<T>추가되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다. 를 호출 TrimExcess하여 용량을 줄일 수 있습니다.
Queue<T> 는 null
참조 형식에 유효한 값으로 허용되고 중복 요소를 허용합니다.
생성자
Queue<T>() |
비어 있는 상태에서 기본 초기 용량을 가지는 Queue<T> 클래스의 새 인스턴스를 초기화합니다. |
Queue<T>(IEnumerable<T>) |
지정된 컬렉션에서 복사한 요소를 포함하고 복사한 요소를 모두 수용할 수 있을 정도의 용량을 가진 Queue<T> 클래스의 새 인스턴스를 초기화합니다. |
Queue<T>(Int32) |
비어 있는 상태에서 지정한 초기 용량을 가지는 Queue<T> 클래스의 새 인스턴스를 초기화합니다. |
속성
Count |
Queue<T>에 포함된 요소 수를 가져옵니다. |
메서드
Clear() |
Queue<T>에서 개체를 모두 제거합니다. |
Contains(T) |
Queue<T>에 요소가 있는지 여부를 확인합니다. |
CopyTo(T[], Int32) | |
Dequeue() |
Queue<T>의 시작 부분에서 개체를 제거하고 반환합니다. |
Enqueue(T) |
개체를 Queue<T>의 끝 부분에 추가합니다. |
EnsureCapacity(Int32) |
이 큐의 용량이 지정된 |
Equals(Object) |
지정된 개체가 현재 개체와 같은지 확인합니다. (다음에서 상속됨 Object) |
GetEnumerator() |
Queue<T>를 반복하는 열거자를 반환합니다. |
GetHashCode() |
기본 해시 함수로 작동합니다. (다음에서 상속됨 Object) |
GetType() |
현재 인스턴스의 Type을 가져옵니다. (다음에서 상속됨 Object) |
MemberwiseClone() |
현재 Object의 단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
Peek() |
Queue<T>의 시작 부분에서 개체를 제거하지 않고 반환합니다. |
ToArray() |
Queue<T> 요소를 새 배열에 복사합니다. |
ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
TrimExcess() |
Queue<T>의 실제 요소 수가 현재 용량의 90% 미만인 경우 용량을 이 값으로 설정합니다. |
TryDequeue(T) |
Queue<T>의 시작 부분에서 개체를 제거하고, 이를 |
TryPeek(T) |
Queue<T>의 시작 부분에 개체가 있는지 여부를 나타내는 값을 반환하고, 개체가 있는 경우 이를 |
명시적 인터페이스 구현
ICollection.CopyTo(Array, Int32) |
특정 ICollection 인덱스부터 시작하여 Array의 요소를 Array에 복사합니다. |
ICollection.IsSynchronized |
ICollection에 대한 액세스가 동기화되어 스레드로부터 안전하게 보호되는지를 나타내는 값을 가져옵니다. |
ICollection.SyncRoot |
ICollection에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다. |
IEnumerable.GetEnumerator() |
컬렉션을 반복하는 열거자를 반환합니다. |
IEnumerable<T>.GetEnumerator() |
컬렉션을 반복하는 열거자를 반환합니다. |
확장 메서드
적용 대상
스레드 보안
공용 정적 (Shared
Visual Basic의)이 형식의 멤버는 스레드로부터 안전 합니다. 인스턴스 구성원은 스레드로부터의 안전성이 보장되지 않습니다.
Queue<T> 지원할 수 있습니다 여러 판독기 동시에 따라 컬렉션을 수정 되지 않습니다. 이 경우에 컬렉션을 열거 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 스레드로부터 안전한 큐는 를 참조하세요 ConcurrentQueue<T>.
피드백
다음에 대한 사용자 의견 제출 및 보기