从列表中删除项目并将它们放在一个数组中

时间:2013-12-12 13:56:36

标签: c# arrays list loops foreach

我想要一个列表并从中随机删除一个元素。

List<int> listArray = new List<int>();
int[] staticArray = new int[8];

listArray.Add(0);
listArray.Add(1);
listArray.Add(2);
listArray.Add(3);
listArray.Add(4);
listArray.Add(5);
listArray.Add(6);
listArray.Add(7);

while (listArray.Any()) {
    int chosen = ra.Next(0, 8);
    int loopIndex = 0;
    bool ok = false;

    if(listArray.Contains(chosen)) {
        ok = true;
    } else {
        ok = false;
        continue;
    }

    foreach (int item in cardIndexes.ToArray()) {
        if (item == chosen && ok == true) {
            staticArray[loopIndex].pathIndex = chosen;
            listArray.Remove(chosen);
            loopIndex++;
            ok = false;
            break;
        }
    }

    if (!cardIndexes.Any()) break;
}

我不明白,这段代码看起来很合乎逻辑。 所以我们将循环遍历列表的副本(以便能够编辑它) 他们选择一个随机数并检查我是否拥有该列表。 如果是,则从列表中删除它并将其分配给静态数组。 然后递增数组的索引以转到另一个元素。 如果我得到一些帮助,我会很高兴。

5 个答案:

答案 0 :(得分:0)

每次从列表中删除项目时,列表大小都会减少一个。

所以这不起作用:

int chosen = ra.Next(0, 8);

答案 1 :(得分:0)

您的代码是无限循环的良好候选者。检查一下:

var cardIndexes = new List<int>
{
    0,
    2,
    3,
    4
};

var nums = new List<int>
{
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8
};

var random = new Random();
while (nums.Any())
{
    var randomVal = nums[random.Next(0, nums.Count)]; // pick random element from nums array.
    nums.Remove(randomVal); // always remove to avoid infinite loop.

    for (var i = 0; i < cardIndexes.Count; i++)
    {
        if (cardIndexes[i] == randomVal)
        {
            // do what you need to do, and break out.
            // staticArray[i].pathIndex = randomVal;
            break;
        }
    }
}

答案 2 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.SqlClient;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

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

            listArray.Add(0);
            listArray.Add(1);
            listArray.Add(2);
            listArray.Add(3);
            listArray.Add(4);
            listArray.Add(5);
            listArray.Add(6);
            listArray.Add(7);

            Random ra = new Random();

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

             //while there is an element in the list
             while (listArray.Any())
             {
             int chosen = ra.Next(0, 8);
             int loopIndex = 0;
             bool ok = false;

    if(listArray.Contains(chosen))
    {
        ok = true;
    }

    else 
    {
        ok = false;
        continue;
    }

     var cardIndexes = from value in listArray
                       where value == chosen
                       select value;


    foreach (int item in cardIndexes.ToArray())
    {
        if (item == chosen && ok == true) 
        {
            staticArray.Add(chosen);
          //  staticArray[loopIndex].pathIndex = chosen;
            listArray.Remove(chosen);
            loopIndex++;
            ok = false;
            break;
        }
    }

    if (!cardIndexes.Any()) break;
                }
        }


    }

}

答案 3 :(得分:0)

List<int> lst = (from l in Enumerable.Range(0, 100) select l).ToList();

List<int> remove = (from rv in Enumerable.Range(0, 10) where rv % 2 == 0 select rv).ToList();

lst.RemoveAll(v => remove.Contains(v));

答案 4 :(得分:0)

        List<int> lst = (from l in Enumerable.Range(0, 100) select l).ToList();
        Random rnd = new Random(Guid.NewGuid().GetHashCode());
        List<int> Chosen = new List<int>();
        while (lst.Any(v => !Chosen.Contains(v)))
        {
            int current = rnd.Next(0, 100);
            if (!Chosen.Contains(current))
            {
                Chosen.Add(current);
            }
        }