如何进入列表中的下一个列表<;列表<;节点>>;当满足特定条件时
本文关键字:列表 gt lt 满足 特定条件 何进入 节点 下一个 | 更新日期: 2023-09-27 18:00:23
我有一个List<Node> nodes
,nodes
中的每个node
都有一个名为Interest
的变量与其关联。
public static List<Node> nodes = new List<Node>();
for (int i = 0; i < Program.n; i++)
{
//int densityrandom = generateRandom();
Node n = new Node(Interest3);
Program.nodes.Add(n);
}
类节点如下:
public class Node
{
public int Density { get; set; }
public InterestProfile P1 { get; set; }
public List<Edge> adjnodes { get; set; }
public Node(InterestProfile a)
{
adjnodes = new List<Edge>();
P1 = a;
}
}
对于InterestProfile
类型的某个对象Interest1
,有一个与之相关联的数组,如
Interest1 = new InterestProfile(array1);
所以,如果你做一些类似nodes[0].P1.InterestArray[0]
的事情,它会给我array1
中的第一个元素。
现在是我的实际问题:
我正在根据用户输入将列表nodes
拆分为节点列表的列表。因此,如果nodes
中有100个元素,则它将被拆分为10个列表,并且所有这些列表将存储在一个列表中。
这是我的想法:
List<List<Node>> smallList = new List<List<Node>>();
for (int i = 0; i < nodes.Count; i++)
{
for(int k=0; k < smallList.Count; k++)
{
if (list[i].P1.InterestArray[0] > 5 && smallList[k].Count != 10)
{
smallList[0].Add(list[i]);
}
else smallList[k+1].Add(list[i]);
}
等等,通过检查与特定节点相关联的阵列中的所有5个元素。然而,当smallList[0]
达到10的极限时,我想开始在其他一些smallList[i]
中添加来自nodes
的元素。当针对nodes
中的所有节点检查特定InterestArray[j]
的所有值时,则只有它应该移动到检查InterestArray[j+1]
。如何在c#中对此进行编程?我的实施是否正确?如果正确,可以改进吗?
我知道这个问题很令人困惑。如果有什么难以理解的地方,请问我。如有任何帮助,我们将不胜感激。
与其创建List<List<Node>>
数据结构,为什么不在需要时简单地查询现有的List<Node>
以查找所需的节点呢?
例如
老派:
var interestingNodes = new List<Node>();
foreach(Node node in nodes)
{
foreach(int i in note.InterestArray)
{
if(i > 5)
interestingNodes.Add(node);
}
}
使用List<T>.FindAll()
:
var interstingNodes = nodes.FindAll(node => node.P1.InterestArray.Contains(5));
或者在LINQ:中
var interstingNodes = from node in nodes
where node.P1.InterestArray.Contains(5)
select node;