集合管理器的代码实现

Posted 爱吃鱼的猫#

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集合管理器的代码实现相关的知识,希望对你有一定的参考价值。

  List<int> a = new List<int>();

            while (true)

            {

                #region//输出集合内容

                Console.WriteLine("集合中现有的内容如下:");

                Console.WriteLine("===================================================================");

                if (a.Count == 0)

                {

                    Console.WriteLine("集合中的没有元素");

                }

                else

                {

                    foreach (int item in a)

                    {

                        Console.Write(item + "\t");

                    }

                    Console.WriteLine();

                }

                Console.WriteLine("===================================================================");

                #endregion

 

                #region//提示菜单,获取用户输入的菜单选项

                Console.WriteLine("1.添加数据");

                Console.WriteLine("2.删除数据");

                Console.WriteLine("3.修改数据");

                Console.WriteLine("4.升序排序");

                Console.WriteLine("0.退出程序");

                Console.Write("请选择(0-4):");

                string input = Console.ReadLine();

                #endregion

 

                #region//根据用户输入的不同,做不同的处理

                if (input == "0")

                {

                    break;

                }

                else if (input == "1")

                {

                    #region//添加数据

                    Console.WriteLine("请输入要添加的数据:");

                    int num = int.Parse(Console.ReadLine());

                    a.Add(num);

                    #endregion

                }

                else if (input == "2")

                {

                    #region//删除数据

                    Console.WriteLine("请输入你要删除的数据(只会删除第一个匹配项):");

                    int num = int.Parse(Console.ReadLine());

                    a.Remove(num);

                    

                    #endregion

 

                }

                else if (input == "3")

                {

                    #region//修改数据

                    if (a.Count == 0)

                    {

                        Console.WriteLine("集合中没有任何数据可以修改,按回车键继续");

                        Console.ReadLine();

                    }

                    else

                    {

                        int maxIndex = a.Count - 1;

                        Console.WriteLine("请输入要删除的下标(0-" + maxIndex + ")");

                        int index = int.Parse(Console.ReadLine());

                        if (index < 0 || index > maxIndex)

                        {

                            Console.Write("输入错误,下标超出范围,按回车键继续");

                            Console.ReadLine();

                        }

                        else

                        {

                            Console.Write("请输入新的数据:");

                            int newNum = int.Parse(Console.ReadLine());

                            a[index] = newNum;

                        }

                    }

                    #endregion

 

                }

                else if (input == "4")

                {

                    #region//升序排序

                    for (int i = 0; i < a.Count - 1; i++)

                    {

                        for (int j = i +1; j < a.Count; j++)

                        {

                            if (a[i] > a[j])

                            {

                                int temp = a[i];

                                a[i] = a[j];

                                a[j] = temp;

                            }

                        }

                    }

                    #endregion

 

                }

 

 

                #endregion

 

                //控制台清屏

                Console.Clear();

            }

以上是关于集合管理器的代码实现的主要内容,如果未能解决你的问题,请参考以下文章

集合中Iterator迭代器的使用以及实现原理。

9.0对于java集合的迭代器的底层分析

资源管理器的自动完成功能

Java内存管理-掌握自定义类加载器的实现

JAVA基础——集合Iterator迭代器的实现

c++ 之定制删除器的代码实现(使用仿函数)