Queue<T> 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
개체의 첫 번째 아웃 컬렉션을 나타냅니다.
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
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> . 여러 스레드에서 동시에 컬렉션에 액세스해야 하는 경우 또는 ConcurrentQueue<T> 사용합니다ConcurrentStack<T>.
a 및 해당 요소에 대해 Queue<T> 다음 세 가지 주요 작업을 수행할 수 있습니다.
용량 Queue<T> 은 보유할 수 있는 Queue<T> 요소의 수입니다. 요소가 추가 Queue<T>되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다. 를 호출 TrimExcess하여 용량을 줄일 수 있습니다.
Queue<T> 는 null 참조 형식에 유효한 값으로 허용되며 중복 요소를 허용합니다.
생성자
| Name | Description |
|---|---|
| Queue<T>() |
비어 있고 기본 초기 용량이 Queue<T> 있는 클래스의 새 인스턴스를 초기화합니다. |
| Queue<T>(IEnumerable<T>) |
지정된 컬렉션에서 복사된 요소를 포함하고 복사된 요소 수를 Queue<T> 수용할 수 있는 충분한 용량이 있는 클래스의 새 인스턴스를 초기화합니다. |
| Queue<T>(Int32) |
비어 있고 지정된 초기 용량을 Queue<T> 가진 클래스의 새 인스턴스를 초기화합니다. |
속성
| Name | Description |
|---|---|
| Count |
에 포함된 Queue<T>요소 수를 가져옵니다. |
메서드
| Name | Description |
|---|---|
| Clear() |
에서 모든 개체를 Queue<T>제거합니다. |
| Contains(T) |
요소가 .에 Queue<T>있는지 여부를 확인합니다. |
| CopyTo(T[], Int32) | |
| Dequeue() |
의 시작 부분에 있는 개체를 제거하고 반환합니다 Queue<T>. |
| Enqueue(T) |
의 끝에 개체를 추가합니다 Queue<T>. |
| Equals(Object) |
지정한 개체와 현재 개체가 같은지 여부를 확인합니다. (다음에서 상속됨 Object) |
| GetEnumerator() |
를 반복하는 열거자를 반환합니다 Queue<T>. |
| GetHashCode() |
기본 해시 함수로 작동합니다. (다음에서 상속됨 Object) |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| Peek() |
개체를 제거하지 않고 시작 부분에 있는 Queue<T> 개체를 반환합니다. |
| ToArray() |
요소를 새 배열에 복사합니다 Queue<T> . |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
| TrimExcess() |
현재 용량의 90% 미만인 경우 용량을 실제 요소 Queue<T>수로 설정합니다. |
| TryDequeue(T) |
개체의 시작 부분에 있는 개체를 Queue<T>제거하고 매개 변수에 |
| TryPeek(T) |
개체의 시작 부분에 Queue<T>개체가 있는지 여부를 나타내는 값을 반환하고 개체가 있는 경우 매개 변수에 |
명시적 인터페이스 구현
| Name | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
특정 ICollection 인덱스에서 시작하여 Array 요소를 Array복사합니다. |
| ICollection.IsSynchronized |
ICollection 대한 액세스가 동기화되는지 여부를 나타내는 값을 가져옵니다(스레드로부터 안전). |
| ICollection.SyncRoot |
ICollection대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다. |
| IEnumerable.GetEnumerator() |
컬렉션을 반복하는 열거자를 반환합니다. |
| IEnumerable<T>.GetEnumerator() |
컬렉션을 반복하는 열거자를 반환합니다. |
확장명 메서드
적용 대상
스레드 보안
이 형식의 공용 정적(Shared Visual Basic) 멤버는 스레드로부터 안전합니다. 모든 인스턴스 멤버는 스레드로부터 안전하게 보호되지 않습니다.
컬렉션이 수정되지 않는 한 A Queue<T> 는 여러 판독기를 동시에 지원할 수 있습니다. 그럼에도 불구하고 컬렉션을 열거하는 것은 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 스레드로부터 안전한 큐는 다음을 참조하세요 ConcurrentQueue<T>.