[英]c# how to change array to array or arrays
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 所以每个元素都是一个元素的数组
You can re-project your arrays to a Dictionary<string, int[][]>
like so: 您可以将数组重新投影到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()}
};
(and I guess if you didn't want the local vars, then (我想如果你不想要当地的变种那么
...
{"IVR", new [] { 1, 0, 0, 0 }.Select(_ => new[] {_}).ToArray()},
...
you can do like this using Jagged Arrays
concept: 你可以使用Jagged Arrays
概念做到这一点:
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: 这是MSDN文档:
http://msdn.microsoft.com/en-us/library/2s05feca.aspx http://msdn.microsoft.com/en-us/library/2s05feca.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.