如果 List<Struct> 中的值等于给定值,则设置布尔值
Setting a bool if a value in List<Struct> is equal to a given value
下面的代码将根据 sList 中最后的 Items ID return 判断真假。超出这一点我很难过,我想知道的是如果有任何一个 sList[i].getID = 1
那么它会设置 bool value = true
但是如果所有值都在 sList[i].getID = 0
那么它会设置bool value = false
。我觉得这是一个简单的答案,但我现在的大脑感觉很模糊。
public Class Data
{
public struct Struct
{
public Struct(string s, bool b, Vector2 v, int i)
: this()
{
this.text = s;
this.tf = b;
this.ID = i
this.seg = v;
}
private String text { get; set; }
public String getText { get { return text; } }
private bool tf { get; set }
public bool getTF { get { return tf; } }
private Vector2 seg { get; set; }
public Vector2 getSeg { get { return seg; } }
private int ID { get; set; }
public int getID { get { return ID; } }
}
}
其他class:
List<Data.Struct> sList = new List<Class.Struct>();
bool value = false;
sList.Add(new Data.Struct("Hello", false, new Vector2(150, 150), 0));
sList.Add(new Data.Struct("Cruel", true, new Vector2(150, 150), 1));
sList.Add(new Data.Struct("World", true, new Vector2(150, 150), 0));
for(int i = 0; i < sList.Count; i++)
{
if(sList[i].getID == 1)
{
value = true;
}
else if(sList[i].getID == 0)
{
value = false;
}
}
List<Data.Struct> sList = new List<Class.Struct>();
sList.Add(new Data.Struct("Hello", false, new Vector2(150, 150), 0));
sList.Add(new Data.Struct("Cruel", true, new Vector2(150, 150), 1));
sList.Add(new Data.Struct("World", true, new Vector2(150, 150), 0));
bool value = sList.Any(s => s.getID == 1);
您可能需要在文件顶部添加一个 using
指令才能使其生效。
如果您由于某种原因无法使用 Any()
方法,请执行以下操作:
List<Data.Struct> sList = new List<Class.Struct>();
sList.Add(new Data.Struct("Hello", false, new Vector2(150, 150), 0));
sList.Add(new Data.Struct("Cruel", true, new Vector2(150, 150), 1));
sList.Add(new Data.Struct("World", true, new Vector2(150, 150), 0));
bool value = false;
foreach(var s in sList)
{
if (s.getId == 1)
{
value = true;
break;
}
}
当你得到 true
时,你应该打破循环,如果你在循环后得到 true 值,这意味着至少有一个 getID == 1
。由于您没有打破循环,因此决定是对集合的最后一个元素做出的,并且任何真值都被最后一个项目 getID
覆盖
for(int i = 0; i < sList.Count; i++)
{
if(sList[i].getID == 1)
{
value = true;
break;
}
}
if(value)
{
//There was at least one element having true for getID
}
else
{
//There was not element having true for getID
}
能用linq就可以用Any
bool value = sList.Any(item => item.getID == 1);
使用for
循环,需要先设置flag为false
,只有遇到1
才设置为true
。目前,您在每次通过时都会覆盖您的值。
value = false;
for (int i = 0; i < sList.Count; i++)
{
if (sList[i].getID == 1)
{
value = true;
}
}
这样,一旦设置为 true
,即使以下任何值是 0
,它也不会重置为 false
。
您还可以在 true
之后使用 break
语句来缩短循环的执行时间。由于您确实没有很多数据,因此几乎取决于您的喜好。
作为更简洁的替代方法,您可以使用 LINQ 的 Any
:
bool value = sList.Any(s => s.getID == 1);
这和它读起来差不多,如果列表中的任何值的 ID 等于 1,它将 return true
。
你也可以使用不太干净的负片 All
:
bool value = !sList.All(s => s.getID == 0);
那个是相反的,不太明显。
它只是 return比较列表中的最后一项,因为它在找到所需的项目后会不断迭代列表。每次它循环下一个项目时, value
的值都会被覆盖。所以只保留value
的最后一个值。
你可以 "exit the loop" 当你找到匹配时,像这样:
for(int i = 0; i < sList.Count; i++)
{
if(sList[i].getID == 1)
{
value = true;
break; // exit the loop
}
else if(sList[i].getID == 0)
{
value = false;
}
}
但其实还有更简单的方法。如果任何项与提供的条件匹配,则可枚举集合的 .Any()
扩展方法将 return true
。像这样:
value = sList.Any(x => x.getID == 1);
下面的代码将根据 sList 中最后的 Items ID return 判断真假。超出这一点我很难过,我想知道的是如果有任何一个 sList[i].getID = 1
那么它会设置 bool value = true
但是如果所有值都在 sList[i].getID = 0
那么它会设置bool value = false
。我觉得这是一个简单的答案,但我现在的大脑感觉很模糊。
public Class Data
{
public struct Struct
{
public Struct(string s, bool b, Vector2 v, int i)
: this()
{
this.text = s;
this.tf = b;
this.ID = i
this.seg = v;
}
private String text { get; set; }
public String getText { get { return text; } }
private bool tf { get; set }
public bool getTF { get { return tf; } }
private Vector2 seg { get; set; }
public Vector2 getSeg { get { return seg; } }
private int ID { get; set; }
public int getID { get { return ID; } }
}
}
其他class:
List<Data.Struct> sList = new List<Class.Struct>();
bool value = false;
sList.Add(new Data.Struct("Hello", false, new Vector2(150, 150), 0));
sList.Add(new Data.Struct("Cruel", true, new Vector2(150, 150), 1));
sList.Add(new Data.Struct("World", true, new Vector2(150, 150), 0));
for(int i = 0; i < sList.Count; i++)
{
if(sList[i].getID == 1)
{
value = true;
}
else if(sList[i].getID == 0)
{
value = false;
}
}
List<Data.Struct> sList = new List<Class.Struct>();
sList.Add(new Data.Struct("Hello", false, new Vector2(150, 150), 0));
sList.Add(new Data.Struct("Cruel", true, new Vector2(150, 150), 1));
sList.Add(new Data.Struct("World", true, new Vector2(150, 150), 0));
bool value = sList.Any(s => s.getID == 1);
您可能需要在文件顶部添加一个 using
指令才能使其生效。
如果您由于某种原因无法使用 Any()
方法,请执行以下操作:
List<Data.Struct> sList = new List<Class.Struct>();
sList.Add(new Data.Struct("Hello", false, new Vector2(150, 150), 0));
sList.Add(new Data.Struct("Cruel", true, new Vector2(150, 150), 1));
sList.Add(new Data.Struct("World", true, new Vector2(150, 150), 0));
bool value = false;
foreach(var s in sList)
{
if (s.getId == 1)
{
value = true;
break;
}
}
当你得到 true
时,你应该打破循环,如果你在循环后得到 true 值,这意味着至少有一个 getID == 1
。由于您没有打破循环,因此决定是对集合的最后一个元素做出的,并且任何真值都被最后一个项目 getID
for(int i = 0; i < sList.Count; i++)
{
if(sList[i].getID == 1)
{
value = true;
break;
}
}
if(value)
{
//There was at least one element having true for getID
}
else
{
//There was not element having true for getID
}
能用linq就可以用Any
bool value = sList.Any(item => item.getID == 1);
使用for
循环,需要先设置flag为false
,只有遇到1
才设置为true
。目前,您在每次通过时都会覆盖您的值。
value = false;
for (int i = 0; i < sList.Count; i++)
{
if (sList[i].getID == 1)
{
value = true;
}
}
这样,一旦设置为 true
,即使以下任何值是 0
,它也不会重置为 false
。
您还可以在 true
之后使用 break
语句来缩短循环的执行时间。由于您确实没有很多数据,因此几乎取决于您的喜好。
作为更简洁的替代方法,您可以使用 LINQ 的 Any
:
bool value = sList.Any(s => s.getID == 1);
这和它读起来差不多,如果列表中的任何值的 ID 等于 1,它将 return true
。
你也可以使用不太干净的负片 All
:
bool value = !sList.All(s => s.getID == 0);
那个是相反的,不太明显。
它只是 return比较列表中的最后一项,因为它在找到所需的项目后会不断迭代列表。每次它循环下一个项目时, value
的值都会被覆盖。所以只保留value
的最后一个值。
你可以 "exit the loop" 当你找到匹配时,像这样:
for(int i = 0; i < sList.Count; i++)
{
if(sList[i].getID == 1)
{
value = true;
break; // exit the loop
}
else if(sList[i].getID == 0)
{
value = false;
}
}
但其实还有更简单的方法。如果任何项与提供的条件匹配,则可枚举集合的 .Any()
扩展方法将 return true
。像这样:
value = sList.Any(x => x.getID == 1);