已有56人关注
课本例题:【例7.4】:数组中添加数组 105页
发表在C#图书答疑 2023-02-27 《C#从入门到精通(第4版)》第7章 数组和集合
是否精华
版块置顶:

namespace ConsoleApp456

{

    internal class Program

    {

        static void Main(string[] args)

        {

            int a = 4;

            int[] b = new int[7] {0,1,2,3,7,8,9 };

            int[] c = new int[3] {4,5,6 };

            if (a > (b.Length + c.Length))

            {

                a= b.Length+c.Length-1;

            }

            int[] f = new int[b.Length+c.Length];

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

            {

                if (a >= 0)

                {

                    if (i < a)

                    {

                        f[i] = b[i];

                    }

                    else if (i == a)

                    {

                        for (int j = 0; j <c.Length; j++)

                        {

                            f[i+j] = c[j];

                        }

                    }

                    else if (i > a)

                    {

                        f[i] = b[i-3];

                    }

                }

                else

                {

                    Console.WriteLine();

                }

            }

            Console.WriteLine("原来的数组1是:");

            foreach (int i in b)

            {

                Console.Write(i+"  ");

            }

            Console.WriteLine(); 

            Console.WriteLine("要添加的数组2是:");

            Console.Write("           ");

            foreach (int i in c)

            {

                Console.Write(i + "  ");

            }

            Console.WriteLine() ;

            

            Console.WriteLine("添加后的数组3是:");

            foreach (int i in f)

            {

                Console.Write(i+"  ");

            }

            Console.ReadLine();

        }

    }

}



原来的数组1是:

0  1  2  3  7  8  9

要添加的数组2是:

           4  5  6

添加后的数组3是:

0  1  2  3  4  2  3  7  8  9


分享到:
精彩评论 1
亨瑞_1706227971
学分:224 LV4
2023-02-27
沙发

请教一下:添加后的数组3只插入一个4,没有插入5、6

首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照