CollectionBase 的应用

前几天笔试的时候一个程序题,要用到CollectionBase类,呵呵,应该好好研究一下基础的东西

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{

 

    class Staff
    {

        private string name; //员工姓名

        private int age; //员工年龄

        /// <summary>
        ///
        /// </summary>
        public Staff()
        {
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="age"></param>
        public Staff(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

        /// <summary>
        /// 设置员工姓名
        /// </summary>
        /// <param name="name">姓名</param>
        public void setName(string name)
        {
            this.name = name;

        }

        /// <summary>
        /// 获取员工姓名
        /// </summary>
        /// <returns>姓名</returns>
        public string getName()
        {
            return name;
        }


        /// <summary>
        /// 设置员工年龄
        /// </summary>
        /// <param name="age">员工年龄</param>
        public void setAge(int age)
        {
            this.age = age;
        }

        /// <summary>
        /// 获取员工年龄
        /// </summary>
        /// <returns>员工年龄</returns>
        public int getAge()
        {
            return age;
        }

 


    }


    class StaffColection : CollectionBase
    {
        public Staff this[int index]
        {
            get
            {
                return ((Staff)List[index]);
            }
            set
            {
                List[index] = value;
            }
        }

        public int Add(Staff value)
        {
            return (List.Add(value));
        }

        public int IndexOf(Staff value)
        {
            return (List.IndexOf(value));
        }

        public void Insert(int index, Staff value)
        {
            List.Insert(index, value);
        }

        public void Remove(Staff value)
        {
            List.Remove(value);
        }

        public bool Contains(Staff value)
        {
            // If value is not of type Int16, this will return false.
            return (List.Contains(value));
        }

        protected override void OnInsert(int index, Object value)
        {
            // Insert additional code to be run only when inserting values.
        }

        protected override void OnRemove(int index, Object value)
        {
            // Insert additional code to be run only when removing values.
        }

        protected override void OnSet(int index, Object oldValue, Object newValue)
        {
            // Insert additional code to be run only when setting values.
        }

        protected override void OnValidate(Object value)
        {
            if (value.GetType() != typeof(Staff))
                throw new ArgumentException("value must be of type Staff.", "value");
        }

    }

    class Program
    {

        /// <summary>
        /// 按照给定的行数和列数打钱集合里的员工的姓名
        /// </summary>
        /// <param name="row">行数</param>
        /// <param name="column">列数</param>
        /// <param name="collection">集合</param>
        /// <returns>html字符串</returns>

        public static string printStaffInfo1(int row, int column, CollectionBase collection)
        {

            IEnumerator ie = collection.GetEnumerator();


            string str = "<table>" + "/n";


            if (row == 0 || column == 0)
            {
                return "";
            }

            if (row * column > collection.Count)
            {
                row = collection.Count / column;
            }


            for (int i = 0; i < row; i++)
            {
                str += "<tr>";
                str += "/n";


                int j = 0;

                while (j < column && ie.MoveNext())
                {
                    str += "<td>";
                    str += "/n";

                    Staff staff = (Staff)ie.Current;
                    str += staff.getName();
                    str += "/n";

                    str += "</td>";
                    str += "/n";

                    j++;
                }

                str += "</tr>";
                str += "/n";

            }

            str += "</table>";
            str += "/n";

            return str;

        }

 

 

        /// <summary>
        /// 按照给定的行数和列数打钱集合里的员工的姓名和年龄
        /// </summary>
        /// <param name="row">行数</param>
        /// <param name="column">列数</param>
        /// <param name="collection">集合</param>
        /// <returns>html字符串</returns>

        public static string printStaffInfo2(int row, int column, CollectionBase collection)
        {

            IEnumerator ie = collection.GetEnumerator();


            string str = "<table>" + "/n";


            if (row == 0 || column == 0)
            {
                return "";
            }

            if (row * column > collection.Count)
            {
                row = collection.Count / column;
            }

 

            for (int i = 0; i < row; i++)
            {
                str += "<tr>";
                str += "/n";


                int j = 0;

                while (j < column && ie.MoveNext())
                {
                    str += "<td>";
                    str += "/n";

                    Staff staff = (Staff)ie.Current;
                    str += staff.getName();
                    str += "/n";

                    str += "</td>";
                    str += "/n";

                    j++;
                }

                str += "</tr>";
                str += "/n";

            }


            ie.Reset();

            for (int i = 0; i < row; i++)
            {
                str += "<tr>";
                str += "/n";


                int j = 0;

                while (j < column && ie.MoveNext())
                {
                    str += "<td>";
                    str += "/n";

                    Staff staff = (Staff)ie.Current;
                    str += staff.getAge();
                    str += "/n";

                    str += "</td>";
                    str += "/n";

                    j++;
                }

                str += "</tr>";
                str += "/n";

            }

 


            str += "</table>";
            str += "/n";

            return str;

        }

 

 


        static void Main(string[] args)
        {

            StaffColection arrayList = new StaffColection();
         
           
            arrayList.Add(new Staff("aa", 11));
            arrayList.Add(new Staff("bb", 12));
            arrayList.Add(new Staff("cc", 13));
            arrayList.Add(new Staff("dd", 14));
            arrayList.Add(new Staff("ee", 15));
            arrayList.Add(new Staff("ff", 16));

        


            Console.WriteLine(Program.printStaffInfo1(1, 3,arrayList));

            Console.WriteLine(Program.printStaffInfo2(3, 3, arrayList));

 

        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值