详解C#中一维数组的插入

时间:2019-04-13
本文章向大家介绍详解C#中一维数组的插入,主要包括详解C#中一维数组的插入使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一维数组的插入:

实现效果:在1 2 3 后面插入4

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Array
{
  class Program
  {
    static void Main(string[] args)
    {
      int[] array = new int[] { 1, 2, 3 };
      int[] des = addArray(array, 4, 4); 
      foreach (int item in des)
      {
        Console.WriteLine(item );
      }
      Console.ReadLine();
    }
    static int[] addArray(int[] bornArray, int index, int value)
    {
      ArrayList list = new ArrayList(bornArray );
      if (index <0)
      {
        index =0 ;
      }
      if (index >bornArray .Length -1)
      {
        index = bornArray.Length;
      }
      list.Insert(index ,value );
      int[] des = new int[list.Count ];
      for (int i=0;i<list.Count;i++)
      {
        des[i] = (int)list[i];
      }
      return des;
    }
  }
}