Queue<T> . 이 코드 예제에서는 기본 용량을 사용 하 여 문자열 큐를 만들고 메서드를 사용하여 Enqueue 5 개의 문자열을 큐에 대기 합니다.
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");
-----------------------------------
foreach( string number in numbers )
{
Console.WriteLine(number);
} //출력물
} one,two,three,four,five
}
Dequeue메서드는 첫 번째 문자열을 큐에서 제거 하는 데 사용 됩니다.
Peek메서드는 큐의 다음 항목을 확인 하는 데 사용 되며, 메서드를 사용 하 여 큐를 제거 Dequeue 합니다.
Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}", numbers.Peek());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
//출력물
Dequeuing 'one'
Peek at next item to dequeue: two <- one을 제거했으니 two가 된다
Dequeuing 'two'
ToArray메서드는 배열을 만들고 큐 요소를 복사 하는 데 사용 되며,
이 배열은 Queue<T> IEnumerable<T> 큐의 복사본을 만드는 데 사용 하는 생성자에 전달 됩니다. 복사본의 요소가 표시 됩니다.
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
numbers의 배열을 ToArry로 복사해서 queueCopy로 생성자에 전달된다.
Console.WriteLine("\nContents of the first copy:");
foreach( string number in queueCopy )
{
Console.WriteLine(number);
} // 출력물
Contents of the copy:
three, four, five
큐 크기의 두 배 배열을 만들고, CopyTo 메서드를 사용 하 여 배열 중간에서 시작 하는 배열 요소를 복사 합니다. Queue<T>생성자는 처음에 세 개의 null 요소가 포함 된 큐의 두 번째 복사본을 만드는 데 다시 사용 됩니다.
string[] array2 = new string[numbers.Count * 2];// numbers의 2배 = 6개
numbers.CopyTo(array2, numbers.Count); // CopyTo (T[] array, int arrayIndex);
배열간의 요소(array2)를 복사해서 numbers.Count 인덱스 부터 복사가 시작된다.(중간에서 시작)
Queue<string> queueCopy2 = new Queue<string>(array2); //
처음 세 개의 null 요소가 포함된 큐 queueCopy2를 만든다.
Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
//Contents of the second copy, with duplicates and nulls:
foreach( string number in queueCopy2 )
{
Console.WriteLine(number);
} //출력물
(앞에 3개는 null로 빈칸) three, four, five
Queue<T>.CopyTo(T[] arry, Int arrayIndex)
arrayT[]
Array에서 복사한 요소의 대상인 일차원 Queue<T>입니다. Array에는 0부터 시작하는 인덱스가 있어야 합니다.
arrayIndexInt32
array에서 복사가 시작되는 0부터 시작하는 인덱스입니다.
public void CopyTo (T[] array, int arrayIndex);
Contains메서드는 큐의 첫 번째 복사본에 "4" 라는 문자열이 있음을 표시 하는 데 사용 되며, 그 후에는 Clear 메서드가 복사본을 지우고 Count 속성은 큐가 비어 있음을 표시 합니다.
Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}", queueCopy.Contains("four"));
Console.WriteLine("\nqueueCopy.Clear()");
queueCopy.Clear();
Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
// 출력물
queueCopy.Contains("four") = True
queueCopy.Clear()
queueCopy.Count = 0
Queue<T>.Contains(T item)
itemT
Queue<T>에서 찾을 개체입니다. 참조 형식에 대해 값은 null이 될 수 있습니다.
반환
Boolean
true가 item에 있으면 Queue<T>이고, 그렇지 않으면 false입니다.