C# 如何创建一个嵌套列表,其中每个子列表都是不同的,可以在不删除前一个子列表的情况下调用它?

C# 如何创建一个嵌套列表,其中每个子列表都是不同的,可以在不删除前一个子列表的情况下调用它?,c#,C#,我将尽我最大的努力来描述我想要输出的样子 基本上我想要一个包含数字列表的列表。每个子列表可以是额外的数字字符串,而列表本身可以是子列表的额外列表字符串 这就是我想象列表的样子 List={List[0]={2,4,6,8…etc},List[1]={3,6,9,12…etc},List[2]{4,8,12,16…etc}……。} 问题是列表和子列表需要存储用户想要的尽可能多的信息 我试图使用list和sublist创建一个嵌套列表。它没有按计划工作。如果我使用sublist.Add,子列表会不断

我将尽我最大的努力来描述我想要输出的样子

基本上我想要一个包含数字列表的列表。每个子列表可以是额外的数字字符串,而列表本身可以是子列表的额外列表字符串

这就是我想象列表的样子

List={List[0]={2,4,6,8…etc},List[1]={3,6,9,12…etc},List[2]{4,8,12,16…etc}……。}

问题是列表和子列表需要存储用户想要的尽可能多的信息

我试图使用
list
和sublist创建一个嵌套列表。它没有按计划工作。如果我使用sublist.Add,子列表会不断增长,当我将子列表分配给主列表时,我只是一次又一次地添加相同的列表。如果我使用sublist.Empty,那么前面的主列表将只记住已添加的最新内容。
我知道这令人困惑,但我不知道如何描述错误。

在回答您的上述评论时,我会使用
词典
而不是
列表
。您可以像最初那样使用
列表
,但是
字典
使查找更容易,因为您可以为每个
列表
定义更有意义的内容,而不是跟踪每个
列表
索引。在某些情况下,
string
Enum
键也很有用

我会这样做:

private static void Main()
{
    // The dictionary 'key' contains the 'divisible by' number
    // The dictionary 'value' is the list of numbers divisible by the key
    var results = new Dictionary<int, List<int>>();

    // This list holds the ints that we care about for finding 'divisible by' counts
    var divisors = new List<int> {11, 13};

    var rnd = new Random();
    for (int i = 0; i < 1000; i++)
    {
        // Generate a random number
        var randomNumber = rnd.Next(1, 501);

        // Check if it's divisible by any of the ints we care about
        foreach (var divisor in divisors.Where(d => randomNumber % d == 0))
        {
            // If it is, add or update the list at that key with this number
            if (results.ContainsKey(divisor))
            {
                results[divisor].Add(randomNumber);
            }
            else
            {
                results[divisor] = new List<int> { randomNumber };
            }
        }
    }

    Console.WriteLine("Here is a comparison of the list counts:");

    foreach (var divisor in divisors)
    {
        var countOfThisItem = (results.ContainsKey(divisor))
            ? results[divisor].Count
            : 0;

        Console.WriteLine($"Numbers divisible by {divisor}: {countOfThisItem}");
    }

    Console.Write("\nDone!\nPress any key to quit.");
    Console.ReadKey();
}
private static void Main()
{
//字典“key”包含“可被整除”的数字
//字典“value”是可被键整除的数字列表
var results=newdictionary();
//此列表包含我们在查找“可除”计数时所关心的整数
var除数=新列表{11,13};
var rnd=新随机数();
对于(int i=0;i<1000;i++)
{
//生成一个随机数
var randomNumber=rnd.Next(1501);
//检查它是否可以被我们关心的整数整除
foreach(除数中的var除数,其中(d=>randomNumber%d==0))
{
//如果是,请使用此编号添加或更新该键处的列表
if(results.ContainsKey(除数))
{
结果[除数].加(随机数);
}
其他的
{
结果[除数]=新列表{randomNumber};
}
}
}
WriteLine(“这里是列表计数的比较:”);
foreach(除数中的var除数)
{
var countofthitem=(results.ContainsKey(除数))
?结果[除数].计数
: 0;
WriteLine($“可被{divisitor}:{countofthitem}整除的数字”);
}
控制台。写入(“\n完成!\n按任意键退出。”);
Console.ReadKey();
}

为了完整性,并回答您最初的问题,下面是我将如何使用
列表

private static void Main()
{
//创建包含两个子列表的父列表
//第一个将跟踪可被11整除的数字
//第二个将跟踪可被13整除的数字
var parentList=新列表
{
新列表(),
新名单()
};
var rnd=新随机数();
对于(int i=0;i<1000;i++)
{
//生成一个随机数
var randomNumber=rnd.Next(1501);
如果(随机数%11==0)
{
父列表[0]。添加(随机数);
}
如果(随机数%13==0)
{
父列表[1]。添加(随机数);
}
}
WriteLine(“这里是列表计数的比较:”);
WriteLine($“可被11整除的数字:{parentList[0].Count}”);
WriteLine($“可被13整除的数字:{parentList[1].Count}”);
控制台。写入(“\n完成!\n按任意键退出。”);
Console.ReadKey();
}

这里有一个例子,你有一个随机数目的玩家,每个人都有一个手(由于分裂可能有多个手)。每手牌都有一定数量的牌(值1到13)

请注意,我添加了一些代码以自动为大约25%的手提供一对,并且我添加了代码以在有对时始终拆分,因为否则拆分太少了。:)

还要注意的是,你需要做真正的“卡片数学”,而不是我下面所说的。目前国王是13分,王牌总是1分,等等

private static void Main()
{
    // Players is a list of people playing, each of whom have
    // a list of hands (they may have more than one in the
    // case of a split), and each hand has a list of cards
    var players = new List<List<List<int>>>();

    var rnd = new Random();

    // Generate a random number of players and deal them each a card
    var numPlayers = rnd.Next(1, 51);
    for (int i = 0; i < numPlayers; i++)
    {
        // Generate the first hand for each player
        players.Add(new List<List<int>> {new List<int> {rnd.Next(1, 14)} });
    }

    Console.WriteLine($"Welcome! We have {players.Count} people playing today.");

    // Deal next card to each player, randomly split pairs, and finish each players hand
    foreach (var playerHands in players)
    {
        // Let player split as often as they can (continue to play
        // hands while they have a hand with one card in it)
        while (playerHands.Any(cardsInHand => cardsInHand.Count == 1))
        {
            foreach (var hand in playerHands.Where(cardsInHand => cardsInHand.Count == 1).ToList())
            {
                var cardInHand = hand[0];
                var drawnCard = rnd.Next(1, 14);

                // I added this code to automatically give them a pair 25% of the time 
                var forcePair = rnd.Next(1, 5) % 4 == 0; 
                if (forcePair)
                {
                    drawnCard = cardInHand;
                }

                if (drawnCard == cardInHand) // They drew a pair
                {
                    // Randomly determine if they will split this hand
                    var splitThisHand = rnd.Next(1, 3) % 2 == 0; // If the number is even, we split

                    // Override random determination and always split on pairs
                    splitThisHand = true;

                    if (splitThisHand)
                    {
                        // Create a new hand for this player and add the card to it
                        playerHands.Add(new List<int> { drawnCard });
                    }
                    else
                    {
                        // Add the card to the existing hand
                        hand.Add(drawnCard);
                    }
                }
                else
                {
                    // Add the card to the existing hand
                    hand.Add(drawnCard);
                }
            }                    
        }

        // Finish playing each hand
        foreach (var hand in playerHands)
        {
            var handValue = hand.Sum();

            // hit if less than 17
            while (handValue < 17)
            {
                var drawnCard = rnd.Next(1, 14);
                hand.Add(drawnCard);
                handValue += drawnCard;
            }
        }
    }

    // Output all the players statistics
    for (int i = 0; i < players.Count; i++)
    {
        var playerHands = players[i];
        Console.WriteLine($"Player #{i + 1} has {playerHands.Count} hands");
        for(int h = 0; h < playerHands.Count; h++)
        {
            var hand = playerHands[h];
            Console.WriteLine(
                $" - Hand #{h + 1} has a value of {hand.Sum()}, containing the cards: {string.Join(", ", hand)}");                    
        }
    }

    Console.Write("\nDone!\nPress any key to quit.");
    Console.ReadKey();
}
private static void Main()
{
//玩家是一个玩家列表,每个人都有
//手的列表(他们可能有一个以上的手)
//在拆分的情况下),每手牌都有一张牌列表
var players=新列表();
var rnd=新随机数();
//生成随机数量的玩家,并给他们每人发一张牌
var numPlayers=rnd.Next(1,51);
for(int i=0;icardsInHand.Count==1))
{
foreach(playerHands.Where中的var hand(cardsInHand=>cardsInHand.Count==1.ToList())
{
var cardInHand=hand[0];
var drawnCard=rnd.Next(1,14);
//我添加这段代码是为了在25%的时间内自动给它们一对
变量forcePair=rnd。下一个(1,5)%4==0;
if(钳副)
{
drawnCard=卡迪南德;
}
private static void Main()
{
    // Players is a list of people playing, each of whom have
    // a list of hands (they may have more than one in the
    // case of a split), and each hand has a list of cards
    var players = new List<List<List<int>>>();

    var rnd = new Random();

    // Generate a random number of players and deal them each a card
    var numPlayers = rnd.Next(1, 51);
    for (int i = 0; i < numPlayers; i++)
    {
        // Generate the first hand for each player
        players.Add(new List<List<int>> {new List<int> {rnd.Next(1, 14)} });
    }

    Console.WriteLine($"Welcome! We have {players.Count} people playing today.");

    // Deal next card to each player, randomly split pairs, and finish each players hand
    foreach (var playerHands in players)
    {
        // Let player split as often as they can (continue to play
        // hands while they have a hand with one card in it)
        while (playerHands.Any(cardsInHand => cardsInHand.Count == 1))
        {
            foreach (var hand in playerHands.Where(cardsInHand => cardsInHand.Count == 1).ToList())
            {
                var cardInHand = hand[0];
                var drawnCard = rnd.Next(1, 14);

                // I added this code to automatically give them a pair 25% of the time 
                var forcePair = rnd.Next(1, 5) % 4 == 0; 
                if (forcePair)
                {
                    drawnCard = cardInHand;
                }

                if (drawnCard == cardInHand) // They drew a pair
                {
                    // Randomly determine if they will split this hand
                    var splitThisHand = rnd.Next(1, 3) % 2 == 0; // If the number is even, we split

                    // Override random determination and always split on pairs
                    splitThisHand = true;

                    if (splitThisHand)
                    {
                        // Create a new hand for this player and add the card to it
                        playerHands.Add(new List<int> { drawnCard });
                    }
                    else
                    {
                        // Add the card to the existing hand
                        hand.Add(drawnCard);
                    }
                }
                else
                {
                    // Add the card to the existing hand
                    hand.Add(drawnCard);
                }
            }                    
        }

        // Finish playing each hand
        foreach (var hand in playerHands)
        {
            var handValue = hand.Sum();

            // hit if less than 17
            while (handValue < 17)
            {
                var drawnCard = rnd.Next(1, 14);
                hand.Add(drawnCard);
                handValue += drawnCard;
            }
        }
    }

    // Output all the players statistics
    for (int i = 0; i < players.Count; i++)
    {
        var playerHands = players[i];
        Console.WriteLine($"Player #{i + 1} has {playerHands.Count} hands");
        for(int h = 0; h < playerHands.Count; h++)
        {
            var hand = playerHands[h];
            Console.WriteLine(
                $" - Hand #{h + 1} has a value of {hand.Sum()}, containing the cards: {string.Join(", ", hand)}");                    
        }
    }

    Console.Write("\nDone!\nPress any key to quit.");
    Console.ReadKey();
}