C#中的数组合并和数组拆分

目录

1、数组合并

2、数组拆分


1、数组合并

        数组的合并就是将多个一维数组合并成一个一维数组,或者将多个一维数组合并成一个二维数组或多维数组。
        在合并数组时,如果是将两个一维数组合并成一个一维数组,那么新生成数组的元素个数必须为要合并的两个一维数组元素个数的和。
//先将两个一维数组合并成一个新的一维数组
//再将定义的两个一维数组合并为一个新的二维数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test7_7
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args is null)                           //解除IDE0060
            {
                throw new ArgumentNullException(nameof(args));
            }          
            int[] arr1 = new int[] { 1, 2, 3, 4, 5 };   //定义两个一维数组1、2
            int[] arr2 = new int[] { 6, 7, 8, 9, 10 };
            int n = arr1.Length + arr2.Length;
            int[] arr3 = new int[n];                    //定义新一维数组3     
            for (int i = 0; i < arr3.Length; i++)       //将数组1和2中到新数组3中
            {
                if (i < arr1.Length)
                    arr3[i] = arr1[i];
                else
                    arr3[i] = arr2[i - arr1.Length];
            }
            Console.WriteLine("合并后的一维数组:");     //遍历新数组3
            foreach (int i in arr3)
                Console.Write("{0}", i + " ");
            Console.WriteLine();
            
            int[,] arr4 = new int[2, 5];                //定义一个要合并的二维数组4         
            for (int i = 0; i < arr4.Rank; i++)         //将两个一维数组循环添加到二维数组中
            {
                switch (i)
                {
                    case 0:
                        {
                            for (int j = 0; j < arr1.Length; j++)
                                arr4[i, j] = arr1[j];
                            break;
                        }
                    case 1:
                        {
                            for (int j = 0; j < arr2.Length; j++)
                                arr4[i, j] = arr2[j];
                            break;
                        }
                }
            }
            Console.WriteLine("合并后的二维数组:");             
            for (int i = 0; i < arr4.Rank; i++)         //遍历二维数组4  
            {
                for (int j = 0; j < arr4.GetUpperBound(arr4.Rank - 1) + 1; j++)
                    Console.Write(arr4[i, j] + " ");
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}
/*运行结果:
合并后的一维数组:
1 2 3 4 5 6 7 8 9 10
合并后的二维数组:
1 2 3 4 5
6 7 8 9 10      */

2、数组拆分

        数组的拆分实际上就是将一个一维数组拆分成多个一维数组,或是将多维数组拆分成多个一维数组或多个多维数组。

//数组的拆分
//将一个int类型的二维数组拆分成两个int类型的一维数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test7_8
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args is null)                                           //解除IDE0060
            {
                throw new ArgumentNullException(nameof(args));
            }
            int[,] arr1 = new int[2, 3] { { 1, 3, 5 }, { 2, 4, 6 } };   //定义一个二维数组,并赋值,源数组            
            int[] arr2 = new int[3];                                    //定义两个一维数组,目标数组
            int[] arr3 = new int[3];
            for (int i = 0; i < 2; i++)                                 //遍历源数组,并通过switch case语句按行拆分
            {
                for (int j = 0; j < 3; j++)
                {
                    switch (i)
                    {
                        case 0: arr2[j] = arr1[i, j]; break;			//按行拆分
                        case 1: arr3[j] = arr1[i, j]; break;			
                    }
                }
            }
            Console.WriteLine("数组一:");
            foreach (int n in arr2)								        //遍历目标数组并显示输出
                Console.Write(n + " ");
            Console.WriteLine();
            Console.WriteLine("数组二:");					
            foreach (int n in arr3)
                Console.Write(n + " ");
            Console.WriteLine();
            Console.Read();
        }
    }
}
/*运行结果:
数组一:
1 3 5
数组二:
2 4 6   */

猜你喜欢

转载自blog.csdn.net/wenchm/article/details/131451943
今日推荐