【问题标题】:This for loop loops through the same values more then once?这个 for 循环不止一次地循环遍历相同的值吗?
【发布时间】:2020-06-03 08:47:44
【问题描述】:

为什么这个 for 循环不止一次地循环遍历相同的值?我将类别 Class 添加到代码中,以便您可以看到我的意思是它们是实际列表。这些列表保留了被加扰的值,应该根据它们是否具有相同的前三个数字来解扰

     for(int i = 0; i < categories2.parentCategoryId.Count; i++)
     {
        for (int j = 0; j < categories2.NewCategoryId.Count; j++)
        {
            if(categories2.parentCategoryId[i].Substring(0, 3) == categories2.NewCategoryId[j].Substring(0, 3)){
                   
                Console.Write(categories2.parentCategoryId[i] + " ");
                Console.Write(categories2.parentCategoryName[i] + "´          ");
                Console.Write(categories2.NewCategoryId[j] + " ");
                Console.Write(categories2.NewCategoryName[j] + "\n"); 

            }
        }
     }




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrestaConverter
{
    public class Categories
    {
        public List<string> CategoryName = new List<string>();
        public List<string> CategoryId = new List<string>();

        public List<string> NewCategoryId = new List<string>();
        public List<string> NewCategoryName = new List<string>();

        public List<string> oldCategoryName = new List<string>();
        public List<string> oldCategoryId = new List<string>();
        
        public List<string> parentCategoryId = new List<string>();
        public List<string> parentCategoryName = new List<string>();

        public List<string> childCategoryID = new List<string>();
        public List<string> childCategoryName = new List<string>();

        public Categories()
        {
          

        } 
        public bool checkFirstNumbers(string categoryid,string correctNumber)
        {
            for(int i = 0; i < 2; i++){
                if(categoryid.Substring(i) == correctNumber.Substring(i))
                {
                    return true; 
                }
            }
           
            
            
            return false; 

        }

    }
}

我有 3 个完整的类别,即 3 个包含 6 个不同列表的对象。 Categories2 是一组应该存在于 categories1 之下的子类别的列表。所以这些类别有两种不同的标识方式,分别是 id 或 name,所以我们有 NewCategoryId 和 NewCategoryName。这些列表中没有两个相同的值。但是我们有 parencategoryName 和 parentCategoryId 列表,这些列表保留了 parentcategoryNames 需要显示的次数,以便能够将子类别放在它们下面。现在我们需要比较 NewCategoryID 和 parentCategoryId 以查看在我们的 excel 文档中放置 NewCategoryName 的位置。

【问题讨论】:

  • "this for loop" - 你的意思是 这些 for 循环。您是否尝试过在调试器中单步执行并检查值?
  • 是的,事情是我试图比较这两个值,如你所见,但循环比较它们不止一次
  • 那么ij每次的值是多少?
  • 如果categories2.parentCategoryId.Countcategories2.NewCategoryId.Count 相同,则只需要外部循环并在它们上使用i 变量。
  • 是否应该在某处有categories1?或者......你只是不擅长命名事物

标签: c# for-loop


【解决方案1】:

解释一下,正如我在评论中提到的,如果categories2.parentCategoryId.Countcategories2.NewCategoryId.Count 相同,您的示例代码可能如下所示:

 for(int i = 0; i < categories2.parentCategoryId.Count; i++)
 {
        if(categories2.parentCategoryId[i].Substring(0, 3) == categories2.NewCategoryId[i].Substring(0, 3)){

            Console.Write(categories2.parentCategoryId[i] + " ");
            Console.Write(categories2.parentCategoryName[i] + "´          ");
            Console.Write(categories2.NewCategoryId[i] + " ");
            Console.Write(categories2.NewCategoryName[i] + "\n"); 
    }
 }

您的代码使用嵌套循环,其工作方式如下:

for(int i = 0; i < 10; i++)
{
    // this is run 10 times
    for(int j = 0; j < 10; j++)
    {
       // this runs 10 times for each iteration of i, so all in all 100 times
    }
}

如果我正确理解了您需要的功能并且您使用 List&lt;T&gt; 您可以使用类似:

 for(int i = 0; i < categories2.parentCategoryId.Count; i++)
 {
     var match = categories2.NewCategoryId.FirstOrDefault(stringToCheck => stringToCheck.Substring(0, 3) == categories2.parentCategoryId[i].Substring(0, 3)));
    if(match != null)
    {
            Console.Write(categories2.parentCategoryId[i] + " ");
            Console.Write(categories2.parentCategoryName[i] + "´          ");
            Console.Write(match  + " ");
            Console.Write(match  + "\n"); 
    }
 }

这里应该发生的是循环遍历第一个列表并在每次迭代中检查第二个列表中是否存在具有相同子字符串的项目(FirstOrDefault 要么返回适合结果的第一个项目,要么null 如果没有找到)。 如果找到正确的项目,则运行与 OP 中相同的代码。

希望我没有犯任何错误,我面前没有代码编辑器;)

【讨论】:

  • 是的,这是正确的,问题是我的嵌套循环在那里 quz 它们在列表中是相同的值,但是它们被打乱了所以它们不按顺序
  • 在这种情况下,遍历一个列表并对另一个进行“包含”测试。它实际上仍然是一个嵌套循环,但测试机制不同。您正在尝试测试的不是“序列相等性”而是 “袋子相等性”
  • @JensSvensson 好的,我想我现在明白了,会考虑一下并更新我的答案。这些是列表、数组还是 ObservableCollections?
  • 和@madreflection 如果你只想测试前三个数字,你会怎么做
  • @JensSvensson 和我的回答...这能回答你的问题吗?
猜你喜欢
  • 1970-01-01
  • 2015-01-07
  • 2020-11-17
  • 1970-01-01
  • 2016-01-26
  • 2020-04-06
  • 1970-01-01
  • 2021-11-25
  • 2016-03-03
相关资源
最近更新 更多