c#数组b的多个3个元素

c#数组b的多个3个元素,c#,arrays,C#,Arrays,如何编写函数以获得新的B数组,但放置数组A的多个3元素 int[] a = new int[] {1,2,3,4,5,6,7,8,9,10}; b0 = a1,a2,a3 b1 = a4,a5,a6 b2 = etc... 如果不能使用LINQ,那么扩展方法中的simplefor循环就可以做到: public static int[][] GetChunks(int[] array, int chunkSize) { if(array.Length <

如何编写函数以获得新的B数组,但放置数组A的多个3元素

int[] a = new int[] {1,2,3,4,5,6,7,8,9,10};

b0 = a1,a2,a3
b1 = a4,a5,a6 
b2 = etc...

如果不能使用LINQ,那么扩展方法中的simple
for
循环就可以做到:

    public static int[][] GetChunks(int[] array, int chunkSize)
    {
        if(array.Length < chunkSize)
        {
           throw new ArgumentOutOfRangeException("chunkSize");
        }

        var result = new List<int[]>(array.Length / chunkSize);

        for (var i = 0; i < array.Length; i += chunkSize)
        {
            var chunk = new int[chunkSize + i < array.Length ? chunkSize : (array.Length - i - 1)];
            for (var j = 0; j < chunk.Length; j++)
            {
                chunk[j] = array[i + j];
            }
            result.Add(chunk);
        }

        return result.ToArray();
    }
输出:

Number of chunks : 4
Second element at 3rd chunk is : 8
附言

如果您希望每个数组有不同的变量,您可以简单地执行以下操作:

 var b = chunksOfFixedSize[0];
 var c = chunksOfFixedSize[1];
 var d = chunksOfFixedSize[2];

最简单最丑陋的方式是

var a = new int[] {1,2,3,4,5,6,7,8,9,10};

var b0 = new int[] {a[0], a[1], a[2]};
var b1 = new int[] {a[3], a[4], a[5]};
否则

int[][] b = new int[a.Length][];

        b[0] = a;

通过使用二维数组,您可以选择所需的任何数组并分配它。

仅当您不允许使用Linq时。

int[] a = new int[] {1,2,3,4,5,6,7,8,9,10};
int[,] b;
if(a.Length % 3 != 0)
{
    b = new int[a.Length/3+1,3];
}
else
{
    b = new int[a.Length/3, 3];
}

for(int i = 0; i< a.Length;i++)
{
    b[i/3,i%3] = a[i];
}
int[]a=新的int[]{1,2,3,4,5,6,7,8,9,10};
int[,]b;
如果(a.长度%3!=0)
{
b=新整数[a.长度/3+1,3];
}
其他的
{
b=新整数[a.长度/3,3];
}
for(int i=0;i
以下是一个解决方案,您可以从中获得阵列的精确长度:

int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// if the length % 3 is zero, then it has length / 3 values
// but if it doesn't go without rest, the length of the array in the first dimention is + 1
int[][] b = new int[((a.Length % 3 == 0) ? a.Length / 3 : a.Length / 3 + 1)][];

for (int i = 0; i < b.Length; i++)
{
    // the length of the second dimension of the array is the 3 if it goes throught 3
    // but if it has rest, then the length is the rest
    b[i] = new int[(i + 1) * 3 <= a.Length ? 3 : a.Length % 3];
    int[] bTemp = new int[b[i].Length];
    for (int j = 0; j < b[i].Length; j++)
    {
        bTemp[j] = a[i * 3 + j];
    }
    b[i] = bTemp;
}
int[]a=新的int[]{1,2,3,4,5,6,7,8,9,10};
//如果长度%3为零,则它具有长度/3值
//但是如果它没有休息,那么第一维数组的长度是+1
int[]b=newint[((a.Length%3==0)?a.Length/3:a.Length/3+1)];
for(int i=0;i
  int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  int size = 3;

  // Array of 3 items arrays
  int[][] result = Enumerable
    .Range(0, a.Length / size + (a.Length % size == 0 ? 0 : 1))
    .Select(index => a
       .Skip(index * size)
       .Take(size)
       .ToArray()) 
    .ToArray(); // if you want array of 3-item arrays
试验


可能重复编写您的代码,也许有人可以解决您的问题提示:b0,b1…您可以将它们保存为数组数组或二维数组。然后您可以将其放入循环中,但您可以发挥您的想象力;)至少,您必须尝试…否则您无法学习编程a。长度太大…如果a%3==0,您需要a/3是的,我只是把它作为一个例子。不过谢谢你的澄清。这甚至不会编译…
a%3
->你的意思是
a.Length%3
?是的,这是肯定的,但我认为不需要多维数组,只需要把和1,2,3放在b[0]中;把和4,5,6放在b[1]中等等。@sadux你的意思是
b[0]=1+2+3
会导致
b[0]=6
?不,我的不好..你就是这么写的..好的,我的朋友,谢谢。我现在试着解开代码。@sadux没问题。如果你喜欢这个答案,你可以投票赞成,甚至把它标记为解决方案。@M.Schema我现在就试着写B={012345678,等等
  int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  int size = 3;

  // Array of 3 items arrays
  int[][] result = Enumerable
    .Range(0, a.Length / size + (a.Length % size == 0 ? 0 : 1))
    .Select(index => a
       .Skip(index * size)
       .Take(size)
       .ToArray()) 
    .ToArray(); // if you want array of 3-item arrays
  String report = String.Join(Environment.NewLine,
    result.Select(chunk => String.Join(", ", chunk))); 

  // 1, 2, 3
  // 4, 5, 6
  // 7, 8, 9
  // 10
  Console.Write(report);