簡體   English   中英

如何在C#中使用元組索引隊列

[英]How to index a Queue with tuples in C#

我有以下元組名稱,ID,YOB列表我將它們排隊,但是當我出隊時,我不知道如何像使用List.item1為List編制索引一樣對它們進行索引

using System;
using System.Collections.Generic;
using System.Collections;

public class Example
{
    public static void Main()
    {
        var Name_ID_YOB = new List<Tuple<string,int,int>>(); // YOB is year of birth
        for (int i = 0; i < 10; i++)
        {
            Name_ID_YOB.Add(new Tuple<string, int,int>("Name", i,1998+i));
        }
        Queue MyQueue = new Queue();
        foreach (var tuple in Name_ID_YOB)
        {
            MyQueue.Enqueue(tuple);
        }
        int Total = MyQueue.Count; // Total Item In Queue
        while (MyQueue.Count > 0)
        {
            var NAMEIDYOB = MyQueue.Dequeue();
            Console.WriteLine($"[Console] {NAMEIDYOB}");
            Console.WriteLine($"[Console] {NAMEIDYOB.item1}"); // Expected to print Name
            Console.WriteLine($"[Console] {NAMEIDYOB.item2}"); // Expected to print ID
            Console.WriteLine($"[Console] {NAMEIDYOB.item3}"); // Expected to print YOB
        }
        Console.WriteLine("Dequeue Done");
        Console.ReadLine();
    }   
}

與使用列表一樣使用通用隊列:

var MyQueue = new Queue<Tuple<string,int,int>>();

另外,您還需要以大寫字母開頭的Item1Item2Item3

while (MyQueue.Count > 0)
{
    var NAMEIDYOB = MyQueue.Dequeue();
    Console.WriteLine($"[Console] {NAMEIDYOB}");
    Console.WriteLine($"[Console] {NAMEIDYOB.Item1}"); // Expected to print Name
    Console.WriteLine($"[Console] {NAMEIDYOB.Item2}"); // Expected to print ID
    Console.WriteLine($"[Console] {NAMEIDYOB.Item3}"); // Expected to print YOB
 }

為什么不定義隊列類型?
Queue<Tuple<string, int, int>> MyQueue = new Queue<Tuple<string, int, int>>();

然后,您可以使用

      Console.WriteLine($"[Console] {NAMEIDYOB.Item1}"); // Expected to print Name
      Console.WriteLine($"[Console] {NAMEIDYOB.Item2}"); // Expected to print ID
      Console.WriteLine($"[Console] {NAMEIDYOB.Item3}"); // Expected to print YOB

更換

var NAMEIDYOB = MyQueue.Dequeue();

Tuple<string, int, int> NAMEIDYOB = (Tuple<string, int, int>) MyQueue.Dequeue();

並訪問元組項目NAMEIDYOB.Item1 (帶有大寫字母的項目),例如:

Console.WriteLine($"[Console] {NAMEIDYOB.Item1}"); // Expected to print Name

您必須將Dequeue() Tuple<string, int, int>Tuple<string, int, int>

這是完整的工作示例:

var Name_ID_YOB = new List<Tuple<string, int, int>>(); // YOB is year of birth
for (int i = 0; i < 10; i++)
{
    Name_ID_YOB.Add(new Tuple<string, int, int>("Name", i, 1998 + i));
}
Queue MyQueue = new Queue();
foreach (var tuple in Name_ID_YOB)
{
    MyQueue.Enqueue(tuple);
}
int Total = MyQueue.Count; // Total Item In Queue
while (MyQueue.Count > 0)
{
    var NAMEIDYOB = MyQueue.Dequeue() as Tuple<string, int, int>;
    Console.WriteLine($"[Console] {NAMEIDYOB}");
    Console.WriteLine($"[Console] {NAMEIDYOB.Item1}"); // Expected to print Name
    Console.WriteLine($"[Console] {NAMEIDYOB.Item2}"); // Expected to print ID
    Console.WriteLine($"[Console] {NAMEIDYOB.Item3}"); // Expected to print YOB
}
Console.WriteLine("Dequeue Done");
Console.ReadLine();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM