c#在数组中添加元素.


1.定义一个一位数组,而后修改数组长度.length获取长度再+1,做为新长度.
2.array引用类型.经过new运算符建立素组并将数组元素初始化做为默认值.
3.定义两个一位数组,用来做为元是数组和预备插入数组.修改原数组长度.数组

文章来自codego.net/tags/1/1/
static int[] AddArray(int[] ArrayBorn, int Index, int Value)ide

        {.net

            if (Index >= (ArrayBorn.Length))//判断添加索引是否大于数组的长度code

                Index = ArrayBorn.Length - 1;//将添加索引设置为数组的最大索引索引

            int[] TemArray = new int[ArrayBorn.Length + 1];//声明一个新的数组it

            for (int i = 0; i < TemArray.Length; i++)//遍历新数组的元素class

            {遍历

                if (Index >= 0)//判断添加索引是否大于等于0引用

                {static

                    if (i < (Index + 1))//判断遍历到的索引是否小于添加索引加1

                        TemArray[i] = ArrayBorn[i];//交换元素值 codego.net/tags/1/1/

                    else if (i == (Index + 1))//判断遍历到的索引是否等于添加索引加1

                        TemArray[i] = Value;//为遍历到的索引设置添加值

                    else

                        TemArray[i] = ArrayBorn[i - 1];//交换元素值

                }

                else

                {

                    if (i == 0)//判断遍历到的索引是否为0

                        TemArray[i] = Value;//为遍历到的索引设置添加值

                    else

                        TemArray[i] = ArrayBorn[i - 1];//交换元素值

                }

            }

            return TemArray;//返回插入元素后的新数组

        }

相关文章
相关标签/搜索