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

c# how to change array to array or arrays
2021-03-07
  •  译文(汉语)
  •  原文(英语)

我有以下代码:

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

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

速聊1:
-Kashtan是将c#对象更改为json的库.物体是什么都没有关系.它可以将任何对象更改为json对象.它是json.net库.但是我的问题是如何使c#对象包含数组数组,而不仅仅是数组.抓到我了吗?
速聊2:
你问如何使之产生的一组重写代码int[][],而不是int[],或询问如何写代码转换给定的int[]int[][]?
速聊3:
-Kashtan第一个,很热心地将int []更改为int [] [],我试图这样做:int[][] ivrArray = { [1], [0], [0], [0]};但是我遇到了语法错误
解决过程1

您可以将数组重新投影为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()},
 ...
速聊1:
我可以说Dictionary<string, int[][]> dictionary代替var dictionary吗?
速聊2:
是的,var是的,是的,尽管该变量仍然被正确正确地键入为Dictionary<string, int[][]>:)
速聊3:
解决过程2

您可以使用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

速聊1:
这样,我在字典中失去了标签
速聊2:
您需要制作字典,例如:<string,int [] []>
速聊3:
正如strautLC还发布的

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

Talk1:
-Kashtan that is a library that changes that c# object to json. it does't matter what the object is. It can change any object to json object. it is json.net library. but my question is how to make that c# object contains array of arrays , not just arrays. got me ?
Talk2:
Are you asking how to rewrite the code so that it creates a set of int[][] instead of int[], or asking how to write code which converts the given int[] to int[][]?
Talk3:
-Kashtan the first one, hot to change int[] to int[][],i tried myself to do like this: int[][] ivrArray = { [1], [0], [0], [0]}; but I got syntax error
Solutions1

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()},
 ...
Talk1:
can i say Dictionary<string, int[][]> dictionary instead or var dictionary ?
Talk2:
Y, sure, although the var variable is still strongly and correctly typed to Dictionary<string, int[][]> :)
Talk3:
could you help me here please stackoverflow.com/questions/23029743/…
Solutions2

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

Talk1:
in this way, i lost the label in the dictinary
Talk2:
you need to make dictionary like: <string,int[][]>
Talk3:
As strautLC also posted
转载于:https://stackoverflow.com/questions/23028526/c-sharp-how-to-change-array-to-array-or-arrays

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入