【发布时间】: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 循环。您是否尝试过在调试器中单步执行并检查值?
-
是的,事情是我试图比较这两个值,如你所见,但循环比较它们不止一次
-
那么
i和j每次的值是多少? -
如果
categories2.parentCategoryId.Count和categories2.NewCategoryId.Count相同,则只需要外部循环并在它们上使用i变量。 -
是否应该在某处有
categories1?或者......你只是不擅长命名事物