目录
向数组中添加数组元素有两种情况:一是在数组中添加一个元素;二是在数组中添加一个数组。
1、向数组中任意位置添加一个元素
//添加数组元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AddItem
{
class Program
{
/// <summary>
/// 增加单个数组元素
/// </summary>
/// <param name="ArrayBorn">要向其中添加元素的一维数组</param>
/// <param name="Index">添加索引</param>
/// <param name="Value">添加值</param>
/// <returns></returns>
static int[] AddArray(int[] ArrayBorn, int Index, int Value)
{
if (Index >= (ArrayBorn.Length)) //如果实参索引是否大于数组的长度
Index = ArrayBorn.Length - 1; //则索引值为数组的最大索引(数组末尾)
int[] TemArray = new int[ArrayBorn.Length + 1]; //声明一个新的数组
for (int i = 0; i < TemArray.Length; i++) //遍历新数组的元素
{
if (Index >= 0) //判断添加索引是否大于等于0
{
if (i < (Index + 1)) //如果实参index值之前含
TemArray[i] = ArrayBorn[i]; //则交换元素值
else if (i == (Index + 1)) //如实参index值之后
TemArray[i] = Value; //则=实参Value(要插入的值)
else
TemArray[i] = ArrayBorn[i - 1]; //否则交换元素值
}
else //否则如实参Index < 0
{
if (i == 0) //如循环变量i=0
TemArray[i] = Value; //则在源数组首位置添加新元素实参值
else
TemArray[i] = ArrayBorn[i - 1]; //然后i!=0时,首位置之后的元素=原数组元素值
}
}
return TemArray; //返回插入元素后的新数组
}
static void Main(string[] args)
{
if (args is null) //解除IDE0060
{
throw new ArgumentNullException(nameof(args));
}
int[] ArrayInt = new int[] { 0, 1, 2, 3, 4, 6, 7, 8, 9 };//声明一个一维数组
Console.WriteLine("原数组元素:");
foreach (int i in ArrayInt) //遍历声明的一维数组
Console.Write(i+" "); //输出数组中的元素
Console.WriteLine(); //换行
ArrayInt = AddArray(ArrayInt, 4, 5);//调用方法向数组中index=4后插入单个元素值=5
Console.WriteLine("插入之后的数组元素:");
foreach (int i in ArrayInt) //遍历插入元素后的一维数组
Console.Write(i+" "); //输出数组中的元素
Console.ReadLine();
}
}
}
/*运行结果:
原数组元素:
0 1 2 3 4 6 7 8 9
插入之后的数组元素:
0 1 2 3 4 5 6 7 8 9 */
2、向指定的一维数组中任意位置添加另一个数组
//向数组中插入一个新的数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AddArrays
{
class Program
{
/// <summary>
/// 在索引值的后面向一维数组中添加一个数组
/// </summary>
/// <param name="ArrayBorn">源数组</param>
/// <param name="ArrayAdd">要添加的数组</param>
/// <param name="Index">添加索引,</param>
/// <returns>新得到的数组</returns>
static int[] AddArray(int[] ArrayBorn, int[] ArrayAdd, int Index)
{
if (Index >= (ArrayBorn.Length)) //如实参Index>数组长度
Index = ArrayBorn.Length - 1; //则插入新数组的位置=数组末尾
int[] TemArray = new int[ArrayBorn.Length + ArrayAdd.Length]; //声明一个新数组确定其长度
for (int i = 0; i < TemArray.Length; i++) //遍历新数组的元素
{
if (Index >= 0) //如输入的实参Index>=0
{
if (i < (Index + 1)) //判断遍历到的索引是否小于添加索引加1
TemArray[i] = ArrayBorn[i]; //交换元素值
else if (i == (Index + 1)) //判断遍历到的索引是否等于添加索引加1
{
for (int j = 0; j < ArrayAdd.Length; j++) //遍历要添加的数组
TemArray[i + j] = ArrayAdd[j];//为遍历到的索引设置添加值
i = i + ArrayAdd.Length - 1; //将遍历索引设置为要添加数组的索引最大值
}
else
TemArray[i] = ArrayBorn[i - ArrayAdd.Length]; //交换其他元素值
}
else //否则,如输入的实参Index<0
{
if (i == 0) //则插入新数组的索引位置=0(把数组插入到这个位置之后)
{
for (int j = 0; j < ArrayAdd.Length; j++) //先按插入数组的长度遍历添加数组元素
TemArray[i + j] = ArrayAdd[j];//为遍历到的索引设置添加值
i = i + ArrayAdd.Length - 1; //计算插入数组后局部变量(索引)i的值
Console.WriteLine(i); //测试
}
else
TemArray[i] = ArrayBorn[i - ArrayAdd.Length]; //然后从索引值i开始遍历插入源数组元素
}
}
return TemArray; //返回添加数组后的新数组
}
static void Main(string[] args)
{
if (args is null) //解除IDE0060
{
throw new ArgumentNullException(nameof(args));
}
int[] ArrayInt = new int[] { 0, 1, 2, 3, 8, 9 };//声明一个数组,用来作为源数组
int[] ArrayInt1 = new int[] { 4, 5, 6, 7 }; //声明一个数组,用来作为要添加的数组
Console.WriteLine(ArrayInt.Length); //测试.Length是数组的元素个数,不包括数组末尾的\0
Console.WriteLine(ArrayInt1.Length); //测试
Console.WriteLine("源数组:");
foreach (int i in ArrayInt) //遍历源数组
Console.Write(i+" "); //输出源数组元素
Console.WriteLine();
Console.WriteLine("要添加的数组:");
foreach (int i in ArrayInt1) //遍历要添加的数组
Console.Write(i + " "); //输出要添加的数组中的元素
Console.WriteLine();
/*ArrayInt = AddArray(ArrayInt, ArrayInt1, 3);*///调用方法,从索引值=3位置向数组中添加数组
ArrayInt = AddArray(ArrayInt, ArrayInt1, -1);//插入位置值<0,切换执行
Console.WriteLine("添加后的数组:");
foreach (int i in ArrayInt) //遍历添加后的数组
Console.Write(i+" "); //输出添加后的数组中的元素
Console.ReadLine();
}
}
}
/*运行结果:
6
4
源数组:
0 1 2 3 8 9
要添加的数组:
4 5 6 7
添加后的数组:
0 1 2 3 4 5 6 7 8 9
//切换后运行结果:
6
4
源数组:
0 1 2 3 8 9
要添加的数组:
4 5 6 7
3
添加后的数组:
4 5 6 7 0 1 2 3 8 9*/