C# 拆分数组的方法


一、程序功能

   本程序主要是把一个数组拆分成若干块,具体的算法是这样的:如果数组有若干个元素,用此方法拆分成若干块新数组,用List类存储新拆分出来的数组,并保证每个数组里面至少有一个元素。这样的算法和抽屉原理是类似的。

   具体举例来说,比如原数组有10个元素,分拆成5个小数组,那按照上面的算法,会产生5个小数组,每个数组有2个元素。难点在于当原数组的总元素数不能被分块数整除的时候,该如何处理。还是以10个元素为例,若是分成3块,那会分成3个小数组,前两个数组有3个元素,最后一个数组有10-2*3=4个元素。程序语句maxLength = totalNum - partNum * (parts - 1)就是这么个意思。

二、程序代码

1、splitArray函数代码        

//数组拆分 未定制

        static List<int>[] SplitArray(int[] Source, int parts)

        {

            if (parts <= Source.Length)

            {

                try

                {

                    int totalNum = Source.Length;

                    int partNum = Convert.ToInt32(totalNum / parts);

                    int sourceIndex = 0;

                    int maxLength = totalNum - partNum * (parts - 1);

                    List<int>[] tempArray = new List<int>[parts];

                    for (int i = 0; i < parts; i++)

                    {

                        if (i < parts - 1)

                        {

                            int[] destinArray = new int[partNum];

                            tempArray[i] = new List<int>(partNum);

                            Console.WriteLine("第" + i + "块:");

                            Array.Copy(Source, sourceIndex, destinArray, 0, partNum);

                            PrintArray(destinArray);

 

                            //将数组保存为链表

                            for (int j = 0; j 

                            {

                                tempArray[i].Add(destinArray[j]);

                               

                            }

 

                            List<int> tempList = tempArray[i];

                            foreach(int temp_i in tempList )

                            {

                                Console.WriteLine(temp_i);

                            }

 

                            sourceIndex = sourceIndex + partNum;

                        }

                        else

                        {

                            int[] destinArray = new int[maxLength];

                            tempArray[i] = new List<int>(maxLength);

                            Console.WriteLine("第" + i + "块:");

                            Array.Copy(Source, sourceIndex, destinArray, 0, maxLength);

                            PrintArray(destinArray);

 

                            for (int j = 0; j 

                            {

                                tempArray[i].Add(destinArray[j]);

                                

                            }

 

                            List<int> tempList = tempArray[i];

                            foreach (int temp_i in tempList)

                            {

                                Console.WriteLine(temp_i);

                            }

                        }

                    }

                    return tempArray;

                }

                catch (IndexOutOfRangeException ex)

                {

                    //throw new Exception(ex.Message);

                    Console.WriteLine(ex.Message);

                    throw new Exception(ex.Message);

                }

            }

            else

            {

                Console.WriteLine("分块数不能大于总数");

                return null;

            }

        }

2、主程序代码

   应该注意的是,我用的List[]类存储数组,即是存有若干个列表的数组,跟二维数组有些类似,在遍历打印的时候,必须用一个临时的List对象存放对应的小数组,而不能直接用下标访问该数组。主函数的代码如下:

            int arraySize = 20;

            int[] array = new int[arraySize];

            int parts = 4;

            int max = 8;

 

            List<int>[] myList = new List<int>[parts];

            InitArray(array);

            Console.WriteLine("数组里的元素如下:");

            PrintArray(array);

 

            myList = SplitArray(array, parts);

 

            Console.WriteLine("分块以后的元素如下:");

            for (int i = 0; i < parts; i++)

            {

                List<int> temp = myList[i];

 

                Console.WriteLine("第" + i + "块:");

                foreach (int temp_i in temp)

                {

                    Console.WriteLine(temp_i);

                }

            }

3、另外给出 initArray和printArray的代码,如下:

static void InitArray(int[] arr)

        {

            int j = 0;

            for (int i = 0; i < arr.Length; i++)

            {

                arr[i] = j;

                j++;

            }

        }

        static void PrintArray<T>(T [] arr)

        {

            for (int i = 0; i < arr.Length; i++)

            {

                Console.Write(arr[i] + " ");

            }

            System.Console.WriteLine();

        }

你可能感兴趣的:(C#)