![](/img/trans.png)
[英]Is it possible to listen for function calls in C# (i.e. treat C# function calls as events)?
[英]How or is it possible to store an array of integers into a dictionary in C#? i.e. is <Integer, int[]> possible?
我是C#的新手,我只是在寫一個供個人使用的快速/小型應用程序。 我想有一個哈希表,將枚舉值關聯到包含整數(即int []格式)的數組。 是否可以不借助ArrayList而實現?
我玩過語法,但沒有運氣。 我基本上想要Dictionary<Integer, int[]>
編輯:
我正在寫一個模式匹配算法,枚舉代表三種不同的類型。 我跟蹤大小為6的數組中的整數值以確定預測。
問題的簡化是正在建立一個整數列表,如果我可以在序列的下一部分到達之前對其進行預測,那將是非常有益的。
這是一種非常簡單的算法,除了gcd等一些數學技巧外,不涉及任何復雜的事情。
順便說一句,謝謝您的幫助! –
這是完全正確的:
var dict = new Dictionary<int, int[]>();
dict.Add(1, new int[] {1, 2, 3});
因此,答案是肯定的,您可以做到。
您可以使用Dictionary<int, int[]>
或Dictionary<int, List<int>>
是的,那是可能的。 例:
Dictionary<int, int[]> items = new Dictionary<int, int[]>();
// add an item using a literal array:
items.Add(42, new int[]{ 1, 2, 3 });
// create an array and add:
int[] values = new int[3];
values[0] = 1;
values[1] = 2;
values[2] = 3;
items.Add(4, values);
// get one item:
int[] values = items[42];
枚舉示例:
enum MyEnum
{
Value1,
Value2,
Value3
}
...
var dictionary = new Dictionary<MyEnum, int[]>();
dictionary[MyEnum.Value1] = new int[] { 1, 2, 3 };
dictionary[MyEnum.Value3] = new int[] { 4, 5, 6 };
dictionary[MyEnum.Value3] = new int[] { 7, 8, 9 };
用法:
int i = dictionary[MyEnum.Value1][1]; // i == 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.