如何实现一个通用的数据结构 Table<TRow, TColumn, TValue>

问题描述

我尝试做的:在.NET平台的泛型集合的基础上,实现泛化数据结构Table<R,C,V>,其中:

R - 行键
C - 列键
V - 价值

该数据结构必须提供对存储在键 RC 上的值的访问,复杂度为 O(1)。 通过创建 Table<FootballTeam,Tournament,HashSet<int>> 集合对象演示如何使用此数据结构。 FootballTeam(title,city,foundationYear) Tournament (title,international: boolean,foundationYear) HashSet<int> - 包含给定 FootballTeam 赢得 Tournament 的年份。 也给出一些你自己使用这个数据结构的例子

我不知道接下来该怎么做,如果我做得对,我想在字典中创建一个字典。

我的代码

using System;
using System.Collections.Generic;
using System.Linq;

namespace task5_virobnicha
{
    public class FootballTeam
    {
       public  string title
        {
            get;
            set;
        }
        public string city
        {
            get;
            set;
        }
        public int foundationYear
        {
            get;
            set;
        }
        public FootballTeam(string title,string city,int foundationYear)
        {
            this.title = title;
            this.city = city;
            this.foundationYear = foundationYear;
        }
        public override string ToString()
        {
            return $"{this.title} {this.city} {this.foundationYear}";
        }
    }

    

    public class Tournament
    {
        public string title
        {
            get;
            set;
        }
        public bool international
        {
            get;
            set;
        }
        public int foundationYear
        {
            get;
            set;
        }

        public Tournament(string title,bool international,int foundationYear)
        {
            this.title = title;
            this.international = international;
            this.foundationYear = foundationYear;
        }
        public override string ToString()
        {
            return $"{this.title} {this.international} {this.foundationYear}";
        }
    }





    class Program
    {
        static void Main(string[] args)
        {

            FootballTeam team1 = new FootballTeam("Vorskla","Poltava",1955);
            FootballTeam team2 = new FootballTeam("Desna","Chernihiv",1960);
            FootballTeam team3 = new FootballTeam("Dinamo","Kyiv",1927);
            FootballTeam team4 = new FootballTeam("Zorya","Luhansk",1923);
            FootballTeam team5 = new FootballTeam("Karpati","Lviv",1963);

            Tournament tourn1 = new Tournament("EURO2012",true,2012);
            Tournament tourn2 = new Tournament("UEFA",2010);
            Tournament tourn3 = new Tournament("OblastTourn",false,2014);
            Tournament tourn4 = new Tournament("Champukr",2005);
            Tournament tourn5 = new Tournament("SyhivChamp",2019);

            HashSet<int> myHash1 = new HashSet<int>();
            myHash1.Add(2012);
            myHash1.Add(2013);
            HashSet<int> myHash2 = new HashSet<int>();
            myHash2.Add(2019);
            myHash2.Add(2010);
            myHash2.Add(2015);
            HashSet<int> myHash3 = new HashSet<int>();
            myHash3.Add(2015);
            HashSet<int> myHash4 = new HashSet<int>();
            myHash4.Add(2005);
            myHash4.Add(2015);
            HashSet<int> myHash5 = new HashSet<int>();
            myHash5.Add(2019);
            myHash5.Add(2020);




            FootballTeam[] listofTeams = new FootballTeam[] { team1,team2,team3,team4,team5 };
            for (int i = 0; i < listofTeams.Length; i++)
            {
                Console.WriteLine(listofTeams[i]);
            }
            Console.WriteLine("---------------------------");

            Tournament[] listofTornaments = new Tournament[] { tourn1,tourn2,tourn3,tourn4,tourn5 };
            for (int i = 0; i < listofTornaments.Length; i++)
            {
                Console.WriteLine(listofTornaments[i]);
            }
            Console.WriteLine("---------------------------");

            

            HashSet <int>[] listofHashSet = new HashSet <int>[] { myHash1,myHash2,myHash3,myHash4,myHash5 };


            foreach (int a in myHash1)
            {
                Console.Write(a);
                Console.Write(" ");
                
            }
            Console.WriteLine();
            foreach (int b in myHash2)
            {
                Console.Write(b);
                Console.Write(" ");
            }
            Console.WriteLine();
            foreach (int c in myHash3)
            {
                Console.Write(c);
                Console.Write(" ");
            }
            Console.WriteLine();
            foreach (int d in myHash4)
            {
                Console.Write(d);
                Console.Write(" ");
            }
            Console.WriteLine();
            foreach (int e in myHash5)
            {
                Console.Write(e);
                Console.Write(" ");
            }
            Console.WriteLine();


            Console.WriteLine("---------------------------");
            Console.WriteLine("Work with dictionary");

            Dictionary<FootballTeam,Tournament> My_dict1 =
                       new Dictionary<FootballTeam,Tournament>();

           
           for(int i =0;i< listofTeams.Length;i++)
            {
                My_dict1.Add(listofTeams[i],listofTornaments[i]);
            }

            



           foreach(var item in My_dict1)
           {
                Console.WriteLine(item);
           }
           Console.WriteLine("---------------------------");


            Dictionary<Dictionary<FootballTeam,Tournament>,HashSet<int>> My_dict2 =
                        new Dictionary<Dictionary<FootballTeam,HashSet<int>>();


            foreach (var item in listofHashSet)
            {
                foreach(var item2 in item)
                {
                    Console.Write(item2);
                    Console.Write(" ");
                }
                Console.WriteLine();
                
            }

            Console.WriteLine("---------------------------");

           

            Console.WriteLine();

            

            Console.WriteLine(My_dict1.ToList()[0]);
        }
        
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)