[英]C#: What is the proper data structure? (jagged object “array”)
I have a List
of IWord
objects named taggedInput
. 我有一个名为
taggedInput
的IWord
对象的List
。 I need to associate each element with its own List
of IWord
objects. 我需要将每个元素与其自己的
IWord
对象List
相关联。 For this I need to use a different datatype. 为此,我需要使用其他数据类型。
In this datatype, both the parent list and the child list must be dynamic, and must be populated at run-time. 在此数据类型中,父列表和子列表都必须是动态的,并且必须在运行时填充。
The parent list must allow duplicate values (so it cannot be a Dictionary
object). 父列表必须允许重复的值(因此它不能是
Dictionary
对象)。
I intend to iterate through the parent list, and manipulate (add, remove, reorganize) the elements in the child list. 我打算遍历父级列表,并操纵(添加,删除,重新组织)子级列表中的元素。 Each element in both the parent and child lists must be accessible by its position in the list.
父列表和子列表中的每个元素都必须可以通过其在列表中的位置进行访问。
Here is an image I made to better illustrate what I need: 这是我制作的图像,目的是更好地说明我的需求:
What is the best data type to use for this? 最佳的数据类型是什么? I need something like
List<IWord,List<IWord>>
(but that wouldn't be a list) 我需要类似
List<IWord,List<IWord>>
(但是那不是列表)
I am assuming you cannot change the IWord
interface? 我假设您不能更改
IWord
界面? In that case I would use another type to allow you to form the required relationship ... 在这种情况下,我将使用另一种类型来允许您形成所需的关系...
public class WordWithRelatives
{
public IWord Word {get;set;}
public List<IWord> Relatives {get;set;}
}
Then you simply require a list of the above 然后,您只需要上面的列表
var taggedInput = List<WordWithRelatives>();
You could also make WordWithRelatives
implement IWord
by forwarding the IWord
interface methods to the Word
instance it contains. 您还可以通过将
IWord
接口方法转发到它包含的Word
实例来使WordWithRelatives
实现IWord
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.