【问题标题】:Delete items from a list and put them in an array从列表中删除项目并将它们放入数组中
【发布时间】:2013-12-12 13:56:35
【问题描述】:

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

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;
}

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

【问题讨论】:

  • 这里有什么问题?
  • 上面的代码做的不对。
  • cardIndexes 和 staticArray 缺少声明,你能发布它吗?
  • 你完成了吗:List listArray = new List(); ?
  • 什么是'ra',staticArray/cardIndexs的声明在哪里?

标签: c# arrays list loops foreach


【解决方案1】:

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

所以这行不通:

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

【讨论】:

  • 为什么它不起作用,它只是返回一个介于 0 和 8 之间的 "random" 值。
  • 如果您的意思是列表中可能不存在 if(listArray.Contains(chosen)) { ok = true; } 其他 { 好的 = 假的;继续; } 这段代码正在处理它。
  • @Veleous:这将生成 0 到 7 之间的随机数。
【解决方案2】:

您的代码非常适合无限循环。检查这个:

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;
        }
    }
}

【讨论】:

    【解决方案3】:
    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;
                    }
            }
    
    
        }
    
    }
    

    【讨论】:

      【解决方案4】:
      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));
      

      【讨论】:

        【解决方案5】:
                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);
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-21
          • 2020-05-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-17
          相关资源
          最近更新 更多