简体   繁体   中英

C#: What is the proper data structure? (jagged object “array”)

I have a List of IWord objects named taggedInput . I need to associate each element with its own List of IWord objects. 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).

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)

I am assuming you cannot change the IWord interface? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM