c#笔记(十七)——队列

Posted 无畏先锋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#笔记(十七)——队列相关的知识,希望对你有一定的参考价值。

《第十二讲:C#语言编程》集合
 
课后题:
1. (*)用集合保存4个人的随机投票,列出最终票数及各自的名字
 
2. (* *)在一个有限平面区域上(1000 * 1000)随机生成有序的n个点(用结构体表示点),将其保存在集合中
  (1)输出所有点的坐标信息
  (2)计算有序相邻两点距离之和(先排序,再求距离)
 
 
3. (* * *)队列:仿照模拟经营类游戏中的情形,自定义一个窗口类,实例化一个窗口,一共有10位顾客,每位顾客接受服务的时间为2~3分钟,求15分钟后仍为接受服务的顾客名单。
 
using System;
//非范型集合所在的命名空间
using System.Collections;
//范型集合命名空间
using System.Collections.Generic;
 
namespace Lesson12
{
                 public class PrintTool{
                                 //专门输出队列的元素
                                 public void PrintStack(Stack a){
                                                 foreach (var item in a) {
                                                                 Console.Write (item+"  " );
                                                }
                                }
 
                                 public void PrintQueue(Queue< string> q){
                                                 foreach (var item in q) {
                                                                 Console.Write (item+"  " );
                                                }
                                                 Console.WriteLine ();
                                }
                }
 
                 class MainClass
                {
                                 public static void Main ( string[] args)
                                {
                                                 //集合:把一些能够确定的不同的对象看成一个整体,
                                                 //                就说这个整体是由这些对象的全体构成的集合.
                                                 //元素:集合中每个对象叫做这个集合的元素.
                                                 PrintTool zhangsan = new PrintTool ();
                                                 Stack stack = new Stack ();
                                                 //Push:往集合里放一个东西
                                                stack.Push (1);
                                                stack.Push (5.4f);
                                                stack.Push ("你好~!" );
 
                                                 //可以用foreach访问Stack集合里的元素
                                                zhangsan.PrintStack (stack);
 
                                                 //Pop:返回并移除集合顶部的对象
                                                 object propItem = stack.Pop ();
                                                 Console.WriteLine (propItem);
 
                                                 //可以用foreach访问Stack集合里的元素
                                                zhangsan.PrintStack (stack);
                                                 object peekItem = stack.Peek ();
 
 
                                                 //赋值语句,条件语句
                                                 //stack.Clear ();
                                                 Queue<string > queue = new Queue< string> ();
                                                queue.Enqueue ("张三" );
                                                queue.Enqueue ("李四" );
                                                queue.Enqueue ("王二" );
                                                queue.Enqueue ("麻子" );
                                                queue.Enqueue ("老王" );
                                                zhangsan.PrintQueue (queue);
                                                 //返回队列的第一个元素,并将这个元素从集合中删除
                                                 string dequeueItem = queue.Dequeue ();
                                                 Console.WriteLine (dequeueItem);
                                                zhangsan.PrintQueue (queue);
                                                 //
                                                 //Dictionary:字典集合
                                                 Dictionary<int ,string> students =
                                                                 new Dictionary <int, string> ();
                                                 //往字典里存放值,key是唯一的,不能重复添加key
                                                students.Add (1, "李逵" );
                                                students.Add (2, "张飞" );
                                                students.Add (3, "宋江" );
                                                students.Add (4, "关羽" );
 
                                                 //用foreach迭代集合中的元素
                                                 foreach (var item in students) {
                                                                 Console.WriteLine (item.Value);
                                                }
                                                 //元素的访问:变量名称[key(键)]形式来访问单个value元素
                                                 Console.WriteLine (students [1]);
                                                 //在直接通过索引方式去值的时候,
                                                 //要注意key在存在情况下才能去成功否则或报错
                                                 if (students.ContainsKey (5)) {
                                                                 Console.WriteLine (students [5]);
                                                } else {
                                                                 Console.WriteLine ("key不存在" );
                                                }
 
                                                 string value;
                                                 bool isGet = students.TryGetValue (5, out value);
                                                 Console.WriteLine (isGet + "  " + value);
 
                                                 //移除键以及键所对应的元素
                                                students.Remove (1);
                                                 //用foreach迭代集合中的元素
                                                 foreach (var item in students) {
                                                                 Console.WriteLine (item);
                                                }
                                                 //判断是否存在指定的值
                                                 Console.WriteLine (students.ContainsValue ("张飞"));
 
                                                 //取得集合中的所有键
                                                 Dictionary<int ,string>. KeyCollection keys = students.Keys;
                                                 foreach (var item in keys) {
                                                                 Console.WriteLine (item);
                                                }
 
                                                 // “张三 ,2”
                                    // "李四 ,5"
                                                 // “wanger , 3”
//                                             Dictionary<string,int> voite = new Dictionary<string, int> ();
//                                             for (int i = 0; i < 10; i++) {
//                                                             string name = Console.ReadLine ();
//                                                             if (!voite.ContainsKey (name)) {
//                                                                             voite.Add (name, 1);
//                                                             } else {
//                                                                             voite [name] += 1;
//                                                             }
//                                             }
//
//                                             foreach (var item in voite) {
//                                                             Console.WriteLine (item);
//                                             }
 
                                                 //List<T> 动态数组
                                                 List<int > scores = new List< int> ();
                                                scores.Add (1);
                                                scores.Add (2);
                                                scores.Add (3);
                                                scores.Add (4);
                                                scores.Add (5);
 
                                                 //Insert,从指定的索引位置插入元素
                                                scores.Insert (3, 6);
                                
                                                 //Remove:删除指定元素
                                                scores.Remove (6);
 
                                                 //RemoveAt:删除指定索引位置的元素
 
                                                 //Reverse:
                                                scores.Reverse ();
                                                 //
                                                scores.Sort ();
                                                 foreach (var item in scores) {
                                                                 Console.WriteLine (item);
                                                }
 
 
                                }
                }
}

以上是关于c#笔记(十七)——队列的主要内容,如果未能解决你的问题,请参考以下文章

python第五十七天-- 补上笔记

C#入门经典(v6) 读书笔记 (第一部分 C#语言)

C# 学习笔记 6.委托

C# 学习笔记-- C# 的基础语法

《C#高级编程》读书笔记

C# 学习笔记线程