一个使用Dictionary的小实例

在C#中,使用Dictionary类来管理由键值对组成的集合,这类集合称为字典。

字典最大的特点就是能够根据键来快速查找集合中的值。

下面是一个使用字典的小实例,希望通过这个小实例,能让大家对字典操作有一个初步的了解。下面是完整代码。

//************************************************************  
//  
// Dictionary示例代码  
//  
// Author:三五月儿  
//   
// Date:2014/09/14 
//  
// http://blog.csdn.net/yl2isoft  
//  
//************************************************************  
using System;
using System.Collections.Generic;
using System.Linq;
namespace DictionaryExp
{
    class Program
    {
        static void Main(string[] args)
        {
            //所有班级所有学生成绩报告单
            Dictionary<int, List<ScoreReport>> scoreDictionary = new Dictionary<int, List<ScoreReport>>();
            //将1班所有学生成绩加入字典
            scoreDictionary.Add(1,
                new List<ScoreReport>()
                {
                    new ScoreReport(){Name="三五月儿",ChineseScore=100,MathScore=100,EnglishScore=100},
                    new ScoreReport(){Name="张三",ChineseScore=80,MathScore=78,EnglishScore=91},
                    new ScoreReport(){Name="李四",ChineseScore=90,MathScore=87,EnglishScore=88}
                });
            //将2班所有学生的成绩加入字典
            scoreDictionary.Add(2,
                new List<ScoreReport>()
                {
                    new ScoreReport(){Name="王五",ChineseScore=78,MathScore=88,EnglishScore=98},
                    new ScoreReport(){Name="丁六",ChineseScore=77,MathScore=99,EnglishScore=91},
                    new ScoreReport(){Name="魏源",ChineseScore=45,MathScore=66,EnglishScore=99}
                });
            //将3班所有学生的成绩加入字典
            scoreDictionary.Add(3,
                new List<ScoreReport>()
                {
                    new ScoreReport(){Name="周鹏",ChineseScore=99,MathScore=89,EnglishScore=78},
                    new ScoreReport(){Name="毛钱",ChineseScore=66,MathScore=98,EnglishScore=91},
                    new ScoreReport(){Name="皮蛋",ChineseScore=87,MathScore=69,EnglishScore=88}
                });
 
            //所有班级学生成绩统计报告单
            Dictionary<int, ScoreStatistics> scoreStatisticsDictionary = new Dictionary<int, ScoreStatistics>();
            scoreStatisticsDictionary.Add(1, new ScoreStatistics());
            scoreStatisticsDictionary.Add(2, new ScoreStatistics());
            scoreStatisticsDictionary.Add(3, new ScoreStatistics());
 
            //获取班级Key的集合
            Dictionary<int, List<ScoreReport>>.KeyCollection keyCollection = scoreDictionary.Keys;
            //通过班级Key遍历班级学生成绩
            foreach (var key in keyCollection)
            {
                //班级成绩统计报告单中包含本班级时才继续
                if (scoreStatisticsDictionary.ContainsKey(key))
                {
                    //当前班级所有学生的详细成绩报告单
                    List<ScoreReport> scoreList = new List<ScoreReport>();
                    scoreDictionary.TryGetValue(key, out scoreList);                    
                    if (scoreList != null && scoreList.Count > 0)
                    {//当前班级所有学生的详细成绩报告单中存在数据
                        int count = scoreList.Count;//当前班级学生人数
                        //生成当前班级学生成绩的统计报告单
                        ScoreStatistics scoreStatistics = new ScoreStatistics();
                        scoreStatisticsDictionary.TryGetValue(key, out scoreStatistics);
                        scoreStatistics.ClassId = key;
                        scoreStatistics.TotalChineseScore = scoreList.Sum(it => it.ChineseScore);
                        scoreStatistics.TotalMathScore = scoreList.Sum(it => it.MathScore);
                        scoreStatistics.TotalEnglishScore = scoreList.Sum(it => it.EnglishScore);
                        scoreStatistics.AverageChineseScore = scoreStatistics.TotalChineseScore / count;
                        scoreStatistics.AverageMathScore = scoreStatistics.TotalMathScore / count;
                        scoreStatistics.AverageEnglishScore = scoreStatistics.TotalEnglishScore / count;
                    }
                }
            }
 
            foreach (var s in scoreStatisticsDictionary)
            {
                Console.WriteLine("ClassId = {0},TotalChineseScore = {1},TotalMathScore = {2},TotalEnglishScore = {3},AverageChineseScore = {4},AverageMathScore = {5},AverageEnglishScore = {6}",
                    s.Value.ClassId, s.Value.TotalChineseScore, s.Value.TotalMathScore, s.Value.TotalEnglishScore, s.Value.AverageChineseScore, s.Value.AverageMathScore, s.Value.AverageEnglishScore);
                Console.WriteLine("-------------------------------------------------");
            }
        }
    }
    class ScoreReport
    {
        public string Name { get; set; }
        public int ChineseScore { get; set; }
        public int MathScore { get; set; }
        public int EnglishScore { get; set; }
    }
 
    class ScoreStatistics
    {
        public int ClassId { get; set; }
        public int TotalChineseScore { get; set; }
        public int TotalMathScore { get; set; }
        public int TotalEnglishScore { get; set; }
        public int AverageChineseScore { get; set; }
        public int AverageMathScore { get; set; }
        public int AverageEnglishScore { get; set; }
    }
}
实例中需要定义两个类:

ScoreReport类表示单个学生的成绩报告单,而ScoreStatistics类表示整个班级的成绩统计报告单,统计信息包含班级各科成绩的总分和平均分。


在程序的Main方法中:

首先,实例化字典对象,其中:

Dictionary<int, List<ScoreReport>>类型的字典scoreDictionary用来保存所有班级所有学生的详细成绩报告单,Key为班级Id,Value为List<ScoreReport>类型的集合,该集合保存班级所有学生的成绩报告单。

Dictionary<int, ScoreStatistics>类型的字典scoreStatisticsDictionary用来保存所有班级的成绩统计报告单,Key同样为班级Id,Value为班级的成绩统计报告单,使用ScoreStatistics类型的对象保存。

接着,向字典scoreDictionary与scoreStatisticsDictionary中分别加入三个班级的学生详细成绩报告单及班级成绩统计报告单,此时scoreStatisticsDictionary中加入的班级成绩统计报告单并不包含真实的统计信息,真实的统计信息需要在后面的计算过程中写入。

最后,遍历scoreDictionary字典,依次取出各个班级所有学生的成绩信息并生成班级成绩的统计信息写入scoreDictionary字典并输出,最终的运行效果如下图所示:


图1 程序运行效果图


在我们的实例中使用到了Dictionary类的以下方法:

1 Add方法

使用Add方法将Key-Value对加入字典。

2 ContainsKey方法

使用ContainsKey方法可以确认字典中是否包含指定Key的键值对,若存在,返回true,否则返回false。

3 TryGetValue方法

使用TryGetValue方法获取指定Key对应的Value。

另外,实例中还使用到了Dictionary类的Keys属性,该属性返回字典中所有Key的集合。

 

好了,就到这里了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值