将数组转换成List
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] x = { 1, 2, 3, 4 };
Console.WriteLine("插入前");
PrintArray(x);
try
{
x = InsertNumber(x, 10, 4);
Console.WriteLine("在 Index=4 处插入10后");
PrintArray(x);
x = InsertNumber(x, 100, 0);
Console.WriteLine("在 Index=0 处插入100后");
PrintArray(x);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
///
/// 将value 插入到指定数组的指定的位置
///
/// 指定数组
/// 待插入的元素
/// 插入的位置
///
static int[] InsertNumber(int[] a, int value, int index)
{
try
{
//转换成List
List
//插入
list.Insert(index, value);
//从List
return list.ToArray();
}
catch (Exception e) // 捕获由插入位置非法而导致的异常
{
throw e;
}
}
///
/// 打印数组
///
static void PrintArray(int[] a)
{
foreach (int x in a)
{
Console.Write("{0} ", x);
}
Console.WriteLine();
}
}
}
经常在开发中,会对字符串 进行split 拆分操作.. 得到数组后再去做相应的事情!但有时候,需求决定了 数组的长度 不是固定的, 而C# 数组 是不允许动态添加新的元素的..想要动态添加进数组, 也只能借助 List
string[] KTCodes = new string[0]; //机型逗号拆分
List
ktls.Add(SVKTCode);
KTCodes = ktls.ToArray();