C#在循环中创建新的数组

初学者,求过路大拿们指点。

问题:用户输入很多个整数(数量不定),第一个整数经过运算后产生许多值(数量不定),存入到一个数组中;第二个整数运算后也产生许多值,想存入到新的数组中。以此类推。所以需要的数组的数量不定。

想到的解决办法:
1、想在循环中生成新数组,数组名称与循环次数挂钩。但不知如何操作,网上找遍也没此类方法。
2、list嵌套list,但子list值经过一次循环后,值也没法改动,一改动父类就跟着改了,无法保存下来。

用 List 就好


using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("type some nums");
            Random rnd = new Random();
            string nums = Console.ReadLine();
            string[] nums_arr = nums.Split(new string[] { ",", " " }, StringSplitOptions.None);
            List<List<int>> ints = new List<List<int>>();
            for (int i = 0; i < nums_arr.Length; i++)
            {
                ints.Add(new List<int>());
                int len = rnd.Next(10, 100);
                for (int j = 0; j < len; j++)
                {
                    ints[i].Add(rnd.Next(100));
                }
            }
            Console.WriteLine(String.Format("您一共输入了{0}个数", ints.Count));
            for (int i = 0; i < ints.Count; i++)
            {
                Console.WriteLine(String.Format("您输入的第{0}个数是:{1}", i + 1, nums_arr[i]));
                Console.WriteLine(String.Format("运算结果是 {0}", String.Join(",", ints[i])));
            }
            Console.ReadLine();
        }
    }
}

img

小魔女参考了bing和GPT部分内容调写:
可以使用C#中的List<List>来实现,即定义一个List,里面的元素是List,List中存放的是int类型的值。

例如:

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

for (int i = 0; i < n; i++)
{
    List<int> tempList = new List<int>();
    // 将第i个整数运算后的结果放入tempList中
    list.Add(tempList);
}

上面的代码中,list是一个List,里面的元素是List,每个List里面存放的是int类型的值,每次循环都会创建一个新的List,将第i个整数运算后的结果放入tempList中,然后将tempList添加到list中。
回答不易,记得采纳呀。