Click here to Skip to main content
Click here to Skip to main content

How to Split an Array into Multiple Arrays

, 31 May 2012
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "How to split an array into multiple arrays"

Introduction

These days, I prefer to split arrays with autosize using the latest methodology, and always being as elegant as possible.

Using the Code

My ideas about changing code (different from the original) are:

  1. Avoid unnecessary if statements in for statements.
  2. Give the possibility to extract the splitted arrays to work with.
  3. Avoid null values.
static void Main(string[] args)
        {
            String[] arrayString = new string[] { "1", "2", "3", "4", "5","6","7" };

            List<string[]> splitted = new List<string[]>();

            Action<string[],> Split = (bulkarray, size) =>
                {
                    int i = -1;
                    int div = bulkarray.Length / size + (bulkarray.Length % size > 0 ? 1 : 0);

                    while (++i < div)
                        splitted.Add(bulkarray.Skip(size * i).Take(size).ToArray());
                };

            Split(arrayString, 3);
        }

I hope you find it interesting, because at least if you are learning C#, in this example you can learn several things.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Juan Pablo G.C.
Software Developer Expediteapps
Spain Spain
I'm Electronic Engineer, I did my end degree project at Astrophysical Institute and Tech Institute. I'm HP Procurve AIS and ASE ,Microsoft 3.5 MCTS
I live in Canary Islands ,developing customized solutions
 
I'm developing with WPF4, SL4 MVVM, MVC3 Razor and WP7 projects, more info at my websites. Improving with Android and IOS.
 
Web:
Expediteapps


Take a look to my blog Juan Pablo G.C.
Mareinsula

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 31 May 2012
Article Copyright 2012 by Juan Pablo G.C.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid