Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#嵌套循环-从列表中获取不同的元素(不带Linq)_C#_List_Loops_Nested - Fatal编程技术网

c#嵌套循环-从列表中获取不同的元素(不带Linq)

c#嵌套循环-从列表中获取不同的元素(不带Linq),c#,list,loops,nested,C#,List,Loops,Nested,如果我有一个列表,其中包含一些可能重复的字符串元素,例如“dog”、“cat”、“dog”、“cow”、“cow”、“owl”。 我需要从该列表中获取不同的元素(因此我需要编写所有元素的列表,而不是重复的元素,例如,“dog”、“cat”、“cow”、“owl”)。 一定有办法将其用于嵌套循环,但我遇到了以下问题: IList<string> animals; int a = animals.Count(); for (int i=0; i<a; i++) { f

如果我有一个列表,其中包含一些可能重复的字符串元素,例如
“dog”、“cat”、“dog”、“cow”、“cow”、“owl”
。 我需要从该列表中获取不同的元素(因此我需要编写所有元素的列表,而不是重复的元素,例如,
“dog”、“cat”、“cow”、“owl”
)。 一定有办法将其用于嵌套循环,但我遇到了以下问题:

IList<string> animals;
int a = animals.Count();
for (int i=0; i<a; i++)
   {
    foreach (string animal in animals)
    {
     if (animals[i+1] != animals[i])
      {
       Console.WriteLine(animals[i]);
       }
    }
  }
IList动物;
int a=动物。计数();
对于(inti=0;i使用一个类似于列表的类,但不允许重复

IList<string> animals = new List<string> {"Lion", "Wolf", "Tiger", "Dog", "Cat", "Lion", "Tiger"};
HashSet<string> uniques = new HashSet<string>(animals);
foreach(animal in uniques)
   Console.WriteLIne(animal);
IList动物=新列表{“狮子”、“狼”、“老虎”、“狗”、“猫”、“狮子”、“老虎”};
HashSet uniques=新的HashSet(动物);
foreach(单一动物)
控制台。书写线(动物);
如果要使用循环,可以使用第二个列表简化代码,在该列表中存储唯一值,同时还可以避免错误的唯一性,因为唯一的区别是动物名称的大小写为大写或小写

IList<string> animals = new List<string> {"Lion", "Wolf", "Tiger", "Dog", "Cat", "Lion", "tiger"};
List<string> uniques = new List<string>();
foreach (string animal in animals)
{
    if (!SearchCaseInsensitive(uniques, animal))
        uniques.Add(animal);
}
foreach(string animal in uniques)
    Console.WriteLine(animal);


bool SearchCaseInsensitive(List<string> source, string search)
{
    string lowerCaseSearch = search.ToLower();
    foreach(string animal in source)
        if(animal.ToLower() == lowerCaseSearch)
            return true;
    return false;
}
IList动物=新列表{“狮子”、“狼”、“老虎”、“狗”、“猫”、“狮子”、“老虎”};
List uniques=新列表();
foreach(动物中的字符串动物)
{
如果(!SearchCase不敏感(uniques,动物))
添加(动物);
}
foreach(单纯形字符串动物)
控制台。书写线(动物);
布尔搜索不区分大小写(列表源,字符串搜索)
{
string lowerCaseSearch=search.ToLower();
foreach(源代码中的字符串动物)
if(animal.ToLower()==小写搜索)
返回true;
返回false;
}

实现这一点的一种方法是,每次遇到新集合时,使用循环迭代元素并添加到另一个集合中。例如

var unique = new List<string>();
foreach(var animal in animals)
{
    if(!unique.Contains(animal))
    {
        unique.Add(animal);
    }
}

foreach(var item in unique)
{
    Console.WriteLine(item);
}

var unique=new List();
foreach(动物中的动物变量)
{
如果(!unique.Contains(动物))
{
独特。添加(动物);
}
}
foreach(唯一的变量项)
{
控制台写入线(项目);
}
如果集合包含的元素太多,查找速度更快的词典可能比列表更好

var unique = new Dictionary<string,int>();
foreach(var animal in animals)
{
    if(!unique.ContainsKey(animal))
    {
        unique.Add(animal,1);
    }
}

foreach(var item in unique)
{
    Console.WriteLine(item.Key);
}
var unique=newdictionary();
foreach(动物中的动物变量)
{
如果(!unique.ContainsKey(动物))
{
独特。添加(动物,1);
}
}
foreach(唯一的变量项)
{
控制台。写入线(项。键);
}

我建议使用HashSet

HashSet<string> uniqueAnimals = new HashSet<string>(animals);
HashSet uniqueAnimals=新的HashSet(动物);
列出动物新列表{“狗”、“猫”、“狗”、“牛”、“牛”、“猫头鹰”};//您的动物列表
列表排序=新列表();
foreach(动物中的字符串动物)
{
如果(!sorted.Contains(动物))
{
排序。添加(动物);
}
}
另一个
哈希集
解决方案:

HashSet<string> unique = new HashSet<string>();

foreach(var animal in animals)
  if (unique.Add(animal))
    Console.WriteLine(animal);
HashSet unique=new HashSet();
foreach(动物中的动物变量)
如果(唯一。添加(动物))
控制台。书写线(动物);

您的方法的问题不是嵌套循环…而是打印而不是将Unique添加到跟踪集合中。尝试想象在每个集合中循环时发生的情况。在第一个循环中,您希望与每只动物进行交互。在第二个循环中,您应该在中检查匹配项animals集合,如果没有,则希望将其作为唯一的元素进行跟踪。在首先检查每个元素之前,您无法实际写入控制台。虽然这肯定是解决问题的最简单方法,但它不使用显式嵌套循环,也无法帮助OP了解嵌套循环应该包含什么plish。这似乎是一个家庭作业问题,这个答案不能帮助OP理解他们最初的方法有什么问题。当然,你是正确的@DavidL如果你想学习如何使用循环,这不是一个解决方案,你的评论很有教育意义。这里的其他答案有一个正确的循环示例,但它们和我的一样失败N处理上下CASHEHASSET也是有效的。我被要求在不使用LINQ的情况下以传统的方式做INT,所以我猜KnightFighter的答案比我需要的更适合。但是哈什特也是好的,有用的。谢谢。是的,但是请考虑“狮子”和“狮子”。到目前为止,每个解决方案中都有两种不同的动物。@kalka79欢迎。接受答案表示感谢的最佳方式:)HashSet同样有效。我被要求在不使用Linq的情况下以传统方式进行int运算,所以我想骑士战士的答案更适合我的需要。但是HashSet很好,也很有用。非常感谢。
HashSet<string> unique = new HashSet<string>();

foreach(var animal in animals)
  if (unique.Add(animal))
    Console.WriteLine(animal);