首页 > 其他分享 >一个大数组根据特定大小分割为多个小数组

一个大数组根据特定大小分割为多个小数组

时间:2023-02-09 18:00:09浏览次数:32  
标签:分割 int arr System 特定 数组 using public size

1. Using Skip() and Take() 的普通方法

using System;
using System.Linq;
using System.Collections.Generic;
 
public static class Extensions
{
    public static IEnumerable<IEnumerable<T>> Split<T>(this T[] arr, int size)
    {
        for (var i = 0; i < arr.Length / size + 1; i++) {
            yield return arr.Skip(i * size).Take(size);
        }
    }
}
 
public class Example
{
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int size = 2;
 
        var arrays = arr.Split(size);
 
        foreach (var array in arrays) {
            Console.WriteLine(String.Join(", ", array));
        }
    }
}

2.  Using Skip() and Take() 的Linq select

using System;
using System.Linq;
using System.Collections.Generic;
 
public static class Extensions
{
    public static IEnumerable<IEnumerable<T>> Split<T>(this T[] arr, int size)
    {
        return arr.Select((s, i) => arr.Skip(i * size).Take(size)).Where(a => a.Any());
    }
}
 
public class Example
{
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int size = 2;
 
        var arrays = arr.Split(size);
 
        foreach (var array in arrays) {
            Console.WriteLine(String.Join(", ", array));
        }
    }
}

3.  Using Enumerable.GroupBy Method

using System;
using System.Linq;
 
public class Example
{
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int size = 2;
 
        int i = 0;
        int[][] arrays = arr.GroupBy(s => i++ / size).Select(s => s.ToArray()).ToArray();
 
        foreach (var array in arrays) {
            Console.WriteLine(String.Join(", ", array));
        }
    }
}

 

标签:分割,int,arr,System,特定,数组,using,public,size
From: https://www.cnblogs.com/deepinnet/p/17106549.html

相关文章

  • 连续数组
    给定一个二进制数组 nums ,找到含有相同数量的 0 和 1 的最长连续子数组,并返回该子数组的长度。/***@param{number[]}nums*@return{number}*/constf......
  • 4.4 数组是高效使用内存的基础
    数组是指多个同样数据类型的数据在内存中连续排列的形式。作为数组元素的各个数据会通过连续的编号被区分开来,这个编号称为索引(index)。指定索引后,就可以对该索引所对应地......
  • C语言填空:数组 最大值 最小值
    #include<stdio.h>//输入5个1-9之间的整数,输出其中的最大值和最小值main(){inta[5],i,j,temp,【1】;for(i=0;i<5;i++)scanf("%d",&a[i]);for(i......
  • python多维数组的每列的最值
    python代码实现importnumpyasnpdefmaxmin(array):#求每列的最值maxlist=[]minlist=[]foriinrange(len(array[0])):#行数col=[]......
  • P10:数组传递下标,删除数组数据
    React16基础​​阐述​​​​数组下标的传递​​​​编写deleteItem方法​​​​正式删除数据​​阐述添加服务虽然很美好,但是有时候也需要有些节制。本文就学习如何删除......
  • laravel data_get 函数从多维数组中获取值
    当需要从多维数组中获取值,并且进行判空,赋值默认值的时候,可以使用下面的测试用例。$arr=["name"=>"陶士涵","title"=>["sub"=>"测试"],"test"=>null......
  • JavaScript 索引、关联、对象数组增删改查循环
    阅读目录索引数组增删改查循环关联数组增循环对象增删改查循环JavaScript中数组元素删除1、length2、delete关键字3、栈方法4、队列方法......
  • sort()排序以及多个属性数组对象排序(按条件排序)
    原生排序letarr=[5,2,1,4,9,8]for(leti=0;i<arr.length;i++){for(letj=0;j<arr.length-1;j++){if(arr[j]>......
  • 数组基础知识
    顺序表SeqList.h#define_CRT_SECURE_NO_WARNINGS#ifndef__SEQLIST_H__#define__SEQLIST_H__#include<stdio.h>#include<malloc.h>#include<assert.h>#include<s......
  • 在排序数组中查找元素的第一个和最后一个位置(Leetcode34)
    3.在排序数组中查找元素的第一个和最后一个位置(Leetcode34)给定一个按照升序排列的整数数组nums,和一个目标值target。找出给定目标值在数组中的开始位置和结束位置。如......