我有以下代码:
int[] ivrArray = { 1, 0, 0, 0};
int[] agentsArray = { 0, 2, 0, 0 };
int[] abandonedArray = { 0, 0, 3, 0};
int[] canceledArray = { 0, 0, 0, 4};
Dictionary<string, int[]> dictionary = new Dictionary<string, int[]>()
{
{ "IVR", ivrArray },
{ "Agents", agentsArray },
{ "Abandoned", abandonedArray },
{ "Cancelled", canceledArray },
};
输出是
{
"IVR": [
1,
0,
0,
0
],
"Agents": [
0,
2,
0,
0
],
"Abandoned": [
0,
0,
3,
0
],
"Cancelled": [
0,
0,
0,
4
]
}
无论如何,所以输出将是这样的:
"Cancelled":[
[0],
[0],
[0],
[4]
]
所以每个元素都是一个元素的数组
int[][]
,而不是int[]
,或询问如何写代码转换给定的int[]
到int[][]
?int[][] ivrArray = { [1], [0], [0], [0]};
但是我遇到了语法错误您可以将数组重新投影为Dictionary<string, int[][]>
如下所示:
var dictionary = new Dictionary<string, int[][]>
{
{"IVR", ivrArray.Select(_ => new[] {_}).ToArray()},
{"Agents", agentsArray.Select(_ => new[] {_}).ToArray()},
{"Abandoned", abandonedArray.Select(_ => new[] {_}).ToArray()},
{ "Cancelled",canceledArray.Select(_ => new[] {_}).ToArray()}
};
(而且我想如果您不希望使用本地变量,那么
...
{"IVR", new [] { 1, 0, 0, 0 }.Select(_ => new[] {_}).ToArray()},
...
Dictionary<string, int[][]> dictionary
代替var dictionary
吗?var
是的,是的,尽管该变量仍然被正确正确地键入为Dictionary<string, int[][]>
:)您可以使用Jagged Arrays
概念这样做:
例如:
int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 5, 6, 7, 8};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };
int[][] lists = new int[][] { list1 , list2 , list3 , list4 };
并在字典中:
Dictionary<string, int[][]> items= new Dictionary<string, int[][]>;
这是MSDN文档:
http://msdn.microsoft.com/en-us/library/2s05feca.aspx
I have this code:
int[] ivrArray = { 1, 0, 0, 0};
int[] agentsArray = { 0, 2, 0, 0 };
int[] abandonedArray = { 0, 0, 3, 0};
int[] canceledArray = { 0, 0, 0, 4};
Dictionary<string, int[]> dictionary = new Dictionary<string, int[]>()
{
{ "IVR", ivrArray },
{ "Agents", agentsArray },
{ "Abandoned", abandonedArray },
{ "Cancelled", canceledArray },
};
The output is
{
"IVR": [
1,
0,
0,
0
],
"Agents": [
0,
2,
0,
0
],
"Abandoned": [
0,
0,
3,
0
],
"Cancelled": [
0,
0,
0,
4
]
}
Is there anyway so the output will be like this:
"Cancelled":[
[0],
[0],
[0],
[4]
]
So each element is array of one element
int[][]
instead of int[]
, or asking how to write code which converts the given int[]
to int[][]
?int[][] ivrArray = { [1], [0], [0], [0]};
but I got syntax errorYou can re-project your arrays to a Dictionary<string, int[][]>
like so:
var dictionary = new Dictionary<string, int[][]>
{
{"IVR", ivrArray.Select(_ => new[] {_}).ToArray()},
{"Agents", agentsArray.Select(_ => new[] {_}).ToArray()},
{"Abandoned", abandonedArray.Select(_ => new[] {_}).ToArray()},
{ "Cancelled",canceledArray.Select(_ => new[] {_}).ToArray()}
};
(and I guess if you didn't want the local vars, then
...
{"IVR", new [] { 1, 0, 0, 0 }.Select(_ => new[] {_}).ToArray()},
...
Dictionary<string, int[][]> dictionary
instead or var dictionary
?var
variable is still strongly and correctly typed to Dictionary<string, int[][]>
:)you can do like this using Jagged Arrays
concept:
for example:
int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 5, 6, 7, 8};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };
int[][] lists = new int[][] { list1 , list2 , list3 , list4 };
and in dictionary:
Dictionary<string, int[][]> items= new Dictionary<string, int[][]>;
here is MSDN documentation:
http://msdn.microsoft.com/en-us/library/2s05feca.aspx
本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文