夜光带你走进C# 游戏开发等(六十四)擅长的领域

夜光序言:

 

从前之前,梦心梦如烟,恰如西风恨柳边。白发又添,不归思故颜,莫言重逢泪双面……

 

 

 

 

 

 

 

 

正文:

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FriendList
{
    class Friend
    {
        public Friend(int id, string name)
        {
            this.id = id;
            this.name = name;
        }

        public int id;
        public string name;

        public override string ToString()
        {
            return string.Format("id={0},name={1}",id,name);
        }

    }

    class FriendList
    {
        private FriendList() { }

        private static FriendList instance = null;

        public static FriendList Instance()
        {
            if(instance == null)
            {
                instance = new FriendList();
            }
            return instance;
        }

        //顺序表
        private Friend[] friendList = null;  //我们给一个数组,如果不初始化的话就是空的
        private int n = 0;  //这个表示好友的数量,一开始是0个好友

        public void SetSize(int n)
        {
            friendList = new Friend[n];
        }

        public void AddFriend(int id,string name)
        {
            if( n == friendList.Length ) //夜光:如果等于总长度
            {
                //抛出异常
                throw new Exception("好友列表已经满了~~");  //相当于创建一个exception对象

                Console.WriteLine("好友列表已经满了~~");
                return;
            }

            Friend f = new Friend(id, name);
            friendList[n] = f;
            n++;
        }


        /// <summary>
        /// 这里我们写一个查找的方法
        /// </summary>
        /// <param name="name"></param>
        public int FindFriend(string name)
        {
            //写一个循环
            for(int i = 0; i < n; i++)
            {
                if (friendList[i].name == name)
                {
                    return i; //返回找到的下标序号
                }              
            }  
            return -1; //表示没找到
        }

        /// <summary>
        /// 这里,我们写一个删除的方法
        /// </summary>

        public void DelFriend(int i)  //没有返回值,删完就可以了
        {
            //夜光:这里有一个bug,如果i是-1的话,怎么办
            //所以这里需要做一个检查
            if(i < 0 || i > n) //或者
            {
                //抛出一个异常
                throw new Exception("数组访问越界");
            }


            //5 = [0,4] 解释了: 为什么是n-1
            //不这样写的话,会访问越界
            for (int j = i; j < n-1; j++)
            {
                friendList[j] = friendList[j + 1];
            }
            n--; //之后再减减
        }

       /// <summary>
       /// 根据名称,我们来删除
       /// </summary>
       /// <param name="name"></param>
        public void DelFriend(string name)
        {
           int index = FindFriend(name);  //首先找一下是否存在,我们用index表示
            if(index == -1)  //如果这个好友不存在的话
            {
                throw new Exception("删除失败,好友不存在~");
            }
            DelFriend(index); //再调用上面那个delfriend
        }



        public override string ToString()
        {
            string s = "---------------------------\n";

            for (int i = 0; i < n; i++)
            {
                /*  Console.WriteLine("id={0},name={1}", friendList[i].id,
                     friendList[i].name);*/
                s += friendList[i] + "\n";
            }
            return s;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            FriendList.Instance().SetSize(3);

            try {  //异常,我们try catch一下
            FriendList.Instance().AddFriend(0, "jim");
            FriendList.Instance().AddFriend(1, "kl");
            FriendList.Instance().AddFriend(2, "jl");
                /*            FriendList.Instance().AddFriend(4, "jl1");*/

                Console.WriteLine(FriendList.Instance());
                FriendList.Instance().DelFriend(1);  //这里我们删除1 好友
                Console.WriteLine(FriendList.Instance());

            }
            catch(Exception e) //夜光:很多异常都可以这样写
            {
                Console.WriteLine(e.Message);
            }


            /*FriendList.Instance().PrintFriends();*/
           

            Console.ReadLine();
        }
    }
}

 

程序啊,一般有问题,都是出现在边界的地方

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值