C#中如何改变在阵列或数组 [英] c# how to change array to array or arrays

查看:115
本文介绍了C#中如何改变在阵列或数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的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 },    
            };

的输出是

{
  "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]
]

所以,每个元素都是一个元素的数组

推荐答案

您可以重新计划你的阵列与词典&LT;字符串,INT [] []&GT; 像这样:

You 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()},
 ...

这篇关于C#中如何改变在阵列或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆