用什么样的数据结构

用什么样的数据结构

本文介绍了用什么样的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,我需要跟踪的:




  • 只是一个字符串名称5-6根项目

  • 每根项目需要有不同的标识符类型(int,字符串,浮点,等等)的多个孩子。一个根的所有的孩子将是相同的类型,但每一根都会有不同的孩子类型

  • 用户将需要能够从每一根添加/删除儿童

  • 稍后我会需要单独访问每个孩子和执行字符串操作和分析需要的时候



我想过也许使用字典,其中的关键是一个字符串和值对象的列表。或有对每一根项目的唯一类和每个类将包括儿童的名单。



有没有人有什么好的建议?我还是很新的OOP,请多多包涵!)



感谢


解决方案

 公共接口IRoot {} 

公共类RootItem< T> :IRoot
{
公共字符串名称{;组; }
公开名单< T>儿童{搞定;组; }
}



然后保持一个词典<字符串,IRoot> ; 来保存所有

 词典<字符串,IRoot>头发=新词典<字符串,IRoot>(); 
hair.Add(
新RootItem< INT>()
{
NAME =无,
儿童=新的List< INT>(){1, 2,3,4}
} $ b $二);

hair.Add(
新RootItem<十进制>()
{
NAME =无,
儿童=新的List<十进制>( ){1M,2M,3M,4M}
}
);


I am working on a project where I need to keep track of:

  • 5-6 Root items of just a string name
  • Each root item need to have multiple children of different identifier types (int, string, float, etc). All the children of one root will be the same type but each root will have different children types
  • user will need to be able to add/delete children from each root
  • i will later need to access each children individually and perform string manipulations and parsing when needed

I've thought about maybe using a dictionary where the Key is a string and Values are lists of objects. Or having a unique class for each root item and each class will include a List of children.

Does anyone have any good suggestions? I'm still quite new to OOP, please bear with me :)

Thanks!

解决方案
public interface IRoot {}

public class RootItem<T> : IRoot
{
    public string Name { get; set; }
    public List<T> Children {get; set; }
}

And then keep a Dictionary<string, IRoot> to hold them all.

Dictionary<string, IRoot> hair = new Dictionary<string, IRoot>();
hair.Add(
  new RootItem<int>()
      {
        Name = "None",
        Children = new List<int>() {1, 2, 3, 4}
      }
);

hair.Add(
  new RootItem<decimal>()
      {
        Name = "None",
        Children = new List<decimal>() {1m, 2m, 3m, 4m}
      }
);

这篇关于用什么样的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 09:00