c#如何将数组更改为一个或多个数组

c#如何将数组更改为一个或多个数组,c#,arrays,C#,Arrays,我有以下代码: 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[]>() {

我有以下代码:

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 },    
            };
是否仍然存在这样的输出结果:

"Cancelled":[
   [0],
   [0],
   [0],
   [4]
]

因此每个元素都是一个元素的数组

您可以将数组重新投影到
字典
中,如下所示:

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

您可以使用锯齿状数组来执行此操作概念:

例如:

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[][]>;
字典项=新字典;
以下是文档:


@AvnerShahar Kashtan,一个将c#对象更改为json的库。物体是什么并不重要。它可以将任何对象更改为json对象。它是json.net库。但我的问题是如何使c#对象包含数组数组,而不仅仅是数组。明白了吗?你是在问如何重写代码,让它创建一组
int[][]
而不是
int[]
,还是在问如何编写代码,将给定的
int[]
转换成
int[][]
?@avnershaar Kashtan第一个,很想把int[]改成int[]],我试着自己这样做:
int[][]]ivraray={[1], [0], [0], [0]}
但是我这样就犯了语法错误,我在字典里丢了标签。你需要把字典改成这样:因为strautLC也发了帖子,我可以说
字典字典
,还是
变量字典
?@MarcoDinatsoli,当然,尽管
变量仍然是强而正确地输入到
字典
:)你能帮我一下吗
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[][]>;