如何添加数组到list并读出来

如何添加数组到list并读出来,第1张

把这句话放到foreach循环里:List<string>jieguo2 = new List<string>()\\声明一维数组jieguo2

foreach (Match match in collection)

{

List<string>jieguo2 = new List<string>()\\声明一维数组jieguo2

GroupCollection gc = match.Groups\\正则表达式的组group

jieguo2.Clear() \\清除数组内容

jieguo2.Add(gc["xing"].Value)\\添加元素到一维数组jieguo2

jieguo2.Add(gc["rmb"].Value) \\添加元素到一维数组jieguo2

jieguo2.Add(gc["shuliang"].Value)\\添加元素到一维数组jieguo2

jieguo2.Add(gc["time"].Value)\\添加元素到一维数组jieguo2

jieguo2.Add(gc["kuanshi"].Value) \\添加元素到一维数组jieguo2

jieguo3.Add(jieguo2)\\添加一维数组jieguo2 到 二维数组jieguo3

}

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)

            }

        }

        

        /// <summary>

        /// 将value 插入到指定数组的指定的位置

        /// </summary>

        /// <param name="a">指定数组</param>

        /// <param name="value">待插入的元素</param>

        /// <param name="index">插入的位置</param>

        /// <returns>插入后的数组</returns>

        static int[] InsertNumber(int[] a, int value, int index)

        {

            try

            {

                //转换成List<int>集合

                List<int> list = new List<int>(a)

                //插入

                list.Insert(index, value)

                //从List<int>集合,再转换成数组

                return list.ToArray()

            }

            catch (Exception e)  // 捕获由插入位置非法而导致的异常

            {

                throw e

            }

        }

        

       /// <summary>

        /// 打印数组

        /// </summary>

        static void PrintArray(int[] a)

        {

            foreach (int x in a)

            {

                Console.Write("{0} ", x)

            }

            Console.WriteLine()

        }

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11611539.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存