Queue<T>.Count 속성

정의

Queue<T>에 포함된 요소 수를 가져옵니다.

public:
 property int Count { int get(); };
public int Count { get; }
member this.Count : int
Public ReadOnly Property Count As Integer

속성 값

Int32

Queue<T>에 포함된 요소의 수입니다.

구현

예제

다음 코드 예제에서는 속성을 포함 하 여 제네릭 클래스의 Queue<T> 여러 속성 및 메서드를 보여 줍니다 Count .

코드 예제에서는 기본 용량이 있는 문자열 큐를 만들고 메서드를 Enqueue 사용하여 5개의 문자열을 큐에 대기합니다. 큐의 요소는 열거되며 큐의 상태는 변경되지 않습니다. 이 Dequeue 메서드는 첫 번째 문자열을 큐에서 제거하는 데 사용됩니다. 이 Peek 메서드는 큐에서 다음 항목을 보는 데 사용되며, 이 Dequeue 메서드는 큐를 해제하는 데 사용됩니다.

ToArray 메서드는 배열을 만들고 큐 요소를 복사하는 데 사용되며, 배열은 큐의 복사본을 만드는 데 사용되는 IEnumerable<T>생성자에 전달됩니다Queue<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
 */
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> 요소의 수입니다. Count 에 실제로 있는 요소의 수는 Queue<T>합니다.

용량은 항상 .보다 크거나 같습니다 Count. 요소를 추가하는 동안 용량을 초과하면 Count 이전 요소를 복사하고 새 요소를 추가하기 전에 내부 배열을 자동으로 다시 할당하여 용량이 증가합니다.

이 속성 값을 검색하는 것은 O(1) 연산입니다.

적용 대상