• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C#向数组添加元素和向数组添加另数组

武飞扬头像
wenchm
帮助1

目录

1、向数组中任意位置添加一个元素 

2、向指定的一维数组中任意位置添加另一个数组


向数组中添加数组元素有两种情况:一是在数组中添加一个元素;二是在数组中添加一个数组。

1、向数组中任意位置添加一个元素 

  1.  
    //添加数组元素
  2.  
    using System;
  3.  
    using System.Collections.Generic;
  4.  
    using System.Linq;
  5.  
    using System.Text;
  6.  
     
  7.  
    namespace AddItem
  8.  
    {
  9.  
    class Program
  10.  
    {
  11.  
    /// <summary>
  12.  
    /// 增加单个数组元素
  13.  
    /// </summary>
  14.  
    /// <param name="ArrayBorn">要向其中添加元素的一维数组</param>
  15.  
    /// <param name="Index">添加索引</param>
  16.  
    /// <param name="Value">添加值</param>
  17.  
    /// <returns></returns>
  18.  
    static int[] AddArray(int[] ArrayBorn, int Index, int Value)
  19.  
    {
  20.  
    if (Index >= (ArrayBorn.Length)) //如果实参索引是否大于数组的长度
  21.  
    Index = ArrayBorn.Length - 1; //则索引值为数组的最大索引(数组末尾)
  22.  
    int[] TemArray = new int[ArrayBorn.Length 1]; //声明一个新的数组
  23.  
    for (int i = 0; i < TemArray.Length; i ) //遍历新数组的元素
  24.  
    {
  25.  
    if (Index >= 0) //判断添加索引是否大于等于0
  26.  
    {
  27.  
    if (i < (Index 1)) //如果实参index值之前含
  28.  
    TemArray[i] = ArrayBorn[i]; //则交换元素值
  29.  
    else if (i == (Index 1)) //如实参index值之后
  30.  
    TemArray[i] = Value; //则=实参Value(要插入的值)
  31.  
    else
  32.  
    TemArray[i] = ArrayBorn[i - 1]; //否则交换元素值
  33.  
    }
  34.  
    else //否则如实参Index < 0
  35.  
    {
  36.  
    if (i == 0) //如循环变量i=0
  37.  
    TemArray[i] = Value; //则在源数组首位置添加新元素实参值
  38.  
    else
  39.  
    TemArray[i] = ArrayBorn[i - 1]; //然后i!=0时,首位置之后的元素=原数组元素值
  40.  
    }
  41.  
    }
  42.  
    return TemArray; //返回插入元素后的新数组
  43.  
    }
  44.  
    static void Main(string[] args)
  45.  
    {
  46.  
    if (args is null) //解除IDE0060
  47.  
    {
  48.  
    throw new ArgumentNullException(nameof(args));
  49.  
    }
  50.  
    int[] ArrayInt = new int[] { 0, 1, 2, 3, 4, 6, 7, 8, 9 };//声明一个一维数组
  51.  
    Console.WriteLine("原数组元素:");
  52.  
    foreach (int i in ArrayInt) //遍历声明的一维数组
  53.  
    Console.Write(i " "); //输出数组中的元素
  54.  
    Console.WriteLine(); //换行
  55.  
    ArrayInt = AddArray(ArrayInt, 4, 5);//调用方法向数组中index=4后插入单个元素值=5
  56.  
    Console.WriteLine("插入之后的数组元素:");
  57.  
    foreach (int i in ArrayInt) //遍历插入元素后的一维数组
  58.  
    Console.Write(i " "); //输出数组中的元素
  59.  
    Console.ReadLine();
  60.  
    }
  61.  
    }
  62.  
    }
  63.  
    /*运行结果:
  64.  
    原数组元素:
  65.  
    0 1 2 3 4 6 7 8 9
  66.  
    插入之后的数组元素:
  67.  
    0 1 2 3 4 5 6 7 8 9 */
学新通

2、向指定的一维数组中任意位置添加另一个数组

  1.  
    //向数组中插入一个新的数组
  2.  
    using System;
  3.  
    using System.Collections.Generic;
  4.  
    using System.Linq;
  5.  
    using System.Text;
  6.  
     
  7.  
    namespace AddArrays
  8.  
    {
  9.  
    class Program
  10.  
    {
  11.  
    /// <summary>
  12.  
    /// 在索引值的后面向一维数组中添加一个数组
  13.  
    /// </summary>
  14.  
    /// <param name="ArrayBorn">源数组</param>
  15.  
    /// <param name="ArrayAdd">要添加的数组</param>
  16.  
    /// <param name="Index">添加索引,</param>
  17.  
    /// <returns>新得到的数组</returns>
  18.  
    static int[] AddArray(int[] ArrayBorn, int[] ArrayAdd, int Index)
  19.  
    {
  20.  
    if (Index >= (ArrayBorn.Length)) //如实参Index>数组长度
  21.  
    Index = ArrayBorn.Length - 1; //则插入新数组的位置=数组末尾
  22.  
    int[] TemArray = new int[ArrayBorn.Length ArrayAdd.Length]; //声明一个新数组确定其长度
  23.  
    for (int i = 0; i < TemArray.Length; i ) //遍历新数组的元素
  24.  
    {
  25.  
    if (Index >= 0) //如输入的实参Index>=0
  26.  
    {
  27.  
    if (i < (Index 1)) //判断遍历到的索引是否小于添加索引加1
  28.  
    TemArray[i] = ArrayBorn[i]; //交换元素值
  29.  
    else if (i == (Index 1)) //判断遍历到的索引是否等于添加索引加1
  30.  
    {
  31.  
    for (int j = 0; j < ArrayAdd.Length; j ) //遍历要添加的数组
  32.  
    TemArray[i j] = ArrayAdd[j];//为遍历到的索引设置添加值
  33.  
    i = i ArrayAdd.Length - 1; //将遍历索引设置为要添加数组的索引最大值
  34.  
    }
  35.  
    else
  36.  
    TemArray[i] = ArrayBorn[i - ArrayAdd.Length]; //交换其他元素值
  37.  
    }
  38.  
    else //否则,如输入的实参Index<0
  39.  
    {
  40.  
    if (i == 0) //则插入新数组的索引位置=0(把数组插入到这个位置之后)
  41.  
    {
  42.  
    for (int j = 0; j < ArrayAdd.Length; j ) //先按插入数组的长度遍历添加数组元素
  43.  
    TemArray[i j] = ArrayAdd[j];//为遍历到的索引设置添加值
  44.  
    i = i ArrayAdd.Length - 1; //计算插入数组后局部变量(索引)i的值
  45.  
    Console.WriteLine(i); //测试
  46.  
    }
  47.  
    else
  48.  
    TemArray[i] = ArrayBorn[i - ArrayAdd.Length]; //然后从索引值i开始遍历插入源数组元素
  49.  
    }
  50.  
    }
  51.  
    return TemArray; //返回添加数组后的新数组
  52.  
    }
  53.  
    static void Main(string[] args)
  54.  
    {
  55.  
    if (args is null) //解除IDE0060
  56.  
    {
  57.  
    throw new ArgumentNullException(nameof(args));
  58.  
    }
  59.  
    int[] ArrayInt = new int[] { 0, 1, 2, 3, 8, 9 };//声明一个数组,用来作为源数组
  60.  
    int[] ArrayInt1 = new int[] { 4, 5, 6, 7 }; //声明一个数组,用来作为要添加的数组
  61.  
    Console.WriteLine(ArrayInt.Length); //测试.Length是数组的元素个数,不包括数组末尾的\0
  62.  
    Console.WriteLine(ArrayInt1.Length); //测试
  63.  
    Console.WriteLine("源数组:");
  64.  
    foreach (int i in ArrayInt) //遍历源数组
  65.  
    Console.Write(i " "); //输出源数组元素
  66.  
    Console.WriteLine();
  67.  
    Console.WriteLine("要添加的数组:");
  68.  
    foreach (int i in ArrayInt1) //遍历要添加的数组
  69.  
    Console.Write(i " "); //输出要添加的数组中的元素
  70.  
    Console.WriteLine();
  71.  
    /*ArrayInt = AddArray(ArrayInt, ArrayInt1, 3);*///调用方法,从索引值=3位置向数组中添加数组
  72.  
    ArrayInt = AddArray(ArrayInt, ArrayInt1, -1);//插入位置值<0,切换执行
  73.  
    Console.WriteLine("添加后的数组:");
  74.  
    foreach (int i in ArrayInt) //遍历添加后的数组
  75.  
    Console.Write(i " "); //输出添加后的数组中的元素
  76.  
     
  77.  
     
  78.  
    Console.ReadLine();
  79.  
    }
  80.  
    }
  81.  
    }
  82.  
    /*运行结果:
  83.  
    6
  84.  
    4
  85.  
    源数组:
  86.  
    0 1 2 3 8 9
  87.  
    要添加的数组:
  88.  
    4 5 6 7
  89.  
    添加后的数组:
  90.  
    0 1 2 3 4 5 6 7 8 9
  91.  
    //切换后运行结果:
  92.  
    6
  93.  
    4
  94.  
    源数组:
  95.  
    0 1 2 3 8 9
  96.  
    要添加的数组:
  97.  
    4 5 6 7
  98.  
    3
  99.  
    添加后的数组:
  100.  
    4 5 6 7 0 1 2 3 8 9*/
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcagkj
系列文章
更多 icon
同类精品
更多 icon
继续加载