如何解决如何实现一个通用的数据结构 Table<TRow, TColumn, TValue>
我尝试做的:在.NET平台的泛型集合的基础上,实现泛化数据结构Table<R,C,V>
,其中:
R
- 行键C
- 列键V
- 价值
该数据结构必须提供对存储在键 R
和 C
上的值的访问,复杂度为 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 举报,一经查实,本站将立刻删除。