C#で別のコレクションからキューを作成しますか?
別のコレクションからキューを作成するためのコードは次のとおりです-
例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<string> queue = new Queue<string>();
queue.Enqueue("One");
queue.Enqueue("Two");
queue.Enqueue("Three");
Console.WriteLine("Queue elements...");
foreach(string str in queue) {
Console.WriteLine(str);
}
Console.WriteLine("\nArray elements...");
Queue<string> arr = new Queue<string>(queue.ToArray());
foreach(string str in arr) {
Console.WriteLine(str);
}
}
} 出力
これにより、次の出力が生成されます-
Queue elements... One Two Three Array elements... One Two Three
別の例を見てみましょう-
例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<int> queue = new Queue<int>();
queue.Enqueue(100);
queue.Enqueue(200);
queue.Enqueue(300);
queue.Enqueue(400);
queue.Enqueue(500);
queue.Enqueue(600);
queue.Enqueue(700);
queue.Enqueue(800);
queue.Enqueue(900);
queue.Enqueue(1000);
Console.WriteLine("Queue elements...");
foreach(int val in queue) {
Console.WriteLine(val);
}
Console.WriteLine("\nArray elements...");
Queue<int> arr = new Queue<int>(queue.ToArray());
foreach(int val in arr) {
Console.WriteLine(val);
}
}
} 出力
これにより、次の出力が生成されます-
Queue elements... 100 200 300 400 500 600 700 800 900 1000 Array elements... 100 200 300 400 500 600 700 800 900 1000
-
C#のコレクションから要素を取得する
リストコレクションの例を見てみましょう。 要素を設定しました- List<int> list = new List<int>(); list.Add(20); list.Add(40); list.Add(60); list.Add(80); ここで、リストから最初の要素を取得する必要があるとします。そのためには、このようにインデックスを設定します- int a = list[0]; 以下は、リストコレクションから要素を取得する方法を示す例です- 例 using System; using System.Collections.Generic; class De
-
別の辞書の値からPython辞書を作成するにはどうすればよいですか?
これを行うには、他の辞書を最初の辞書にマージします。 Python 3.5以降では、**演算子を使用して辞書を解凍し、次の構文を使用して複数の辞書を組み合わせることができます- 構文 a = {'foo': 125} b = {'bar': "hello"} c = {**a, **b} print(c) 出力 これにより出力が得られます- {'foo': 125, 'bar': 'hello'} これは古いバージョンではサポートされていません。ただし、次の同様の構文を使用して置き換えることがで