简体   繁体   English

如何将整数数组存储到C#中的字典中? 即是 <Integer, int[]> 可能?

[英]How or is it possible to store an array of integers into a dictionary in C#? i.e. is <Integer, int[]> possible?

I'm new to C# and I'm just writing a quick/small scale app for personal use. 我是C#的新手,我只是在写一个供个人使用的快速/小型应用程序。 I would like to have a Hash table that associates enum values to an array containing integers ie int [] format. 我想有一个哈希表,将枚举值关联到包含整数(即int []格式)的数组。 Is this possible without resorting to using an ArrayList? 是否可以不借助ArrayList而实现?

I played with the syntax and I am having no luck. 我玩过语法,但没有运气。 I basically want Dictionary<Integer, int[]> 我基本上想要Dictionary<Integer, int[]>

EDIT: 编辑:

I'm writing a pattern matching algorithm and there are three different types represented by an enum. 我正在写一个模式匹配算法,枚举代表三种不同的类型。 I am keeping track of integer values in an array of size 6 to determine a prediction. 我跟踪大小为6的数组中的整数值以确定预测。

The simplification of the problem is that there is a list of integers being built up, and it would be greatly beneficial if I could predict the next part of a sequence before it arrives. 问题的简化是正在建立一个整数列表,如果我可以在序列的下一部分到达之前对其进行预测,那将是非常有益的。

It's a very simple algorithm, and doesn't involve anything complex other than a few math tricks like gcd, etc. 这是一种非常简单的算法,除了gcd等一些数学技巧外,不涉及任何复杂的事情。

Btw, thank you for the help! 顺便说一句,谢谢您的帮助!

This is perfectly valid: 这是完全正确的:

var dict = new Dictionary<int, int[]>();
dict.Add(1, new int[] {1, 2, 3});

So the answer would be yes, you can do that. 因此,答案是肯定的,您可以做到。

您可以使用Dictionary<int, int[]>Dictionary<int, List<int>>

Yes, that is possible. 是的,那是可能的。 Example: 例:

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];

Example for enum: 枚举示例:

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 };

Usage: 用法:

int i = dictionary[MyEnum.Value1][1]; // i == 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以侦听C#中的函数调用(即将C#函数调用视为事件)? - Is it possible to listen for function calls in C# (i.e. treat C# function calls as events)? C#中的字典数组-可能吗? - An Array of Dictionary in C# - Possible? 具有通用C#词典的加法(即+)扩展方法 - Addition (i.e. +) extension method with a generic C# Dictionary 是否可以向App_Code添加两个语言代码文件,即C#和VB.NET? 如果是这样,我怎么能做到这一点? - Is it possible to add two language code files i.e. C# and VB.NET to App_Code? If so, how I could achieve this? C#方法具有最大值?即大小(int x <40,int y <80)? - C# Method's with maximum values? i.e. Size(int x < 40, int y < 80)? 是否可以将lambda表达式存储在数组C#中 - Is it possible to store lambda expression in array C# 如何将整数转换为数组(从整数 i 到整数 j)。 C# - How to get integers to an array (from integer i to integer j). c# 如何在C#中通过String调用构造函数(即通过Reflection)? - How to invoke Constructors by String in C# (i.e. by Reflection)? C# - 如何用“-É”替换重音字符,即“-É” - C# - How to replace accented characters, i.e., "-É" with "- É" 有没有可能在c#4.5中存储临时整数值的方法? - is there any possible way to store temporary integer value in c# 4.5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM