C#数组(存储User类型)增、删、改、查

using System;
using System.Collections.Generic;
using System.Text;

namespace ArrayOperate
{
    // 数组(存储User类型)
    class UserList
    {
        private User[] data = null;
        private User[] data_new = null;
        private int capacity; // 容量
        private int size; // 实际大小

        public UserList() : this(2) // 这里默认初始数组大小是两个元素
        {
        }
        public UserList(int capacity)
        {
            data = new User[capacity];
            this.capacity = data.Length;
            this.size = 0;
        }
        public int Capacity
        {
            get { return capacity; }
            set { this.capacity = value; }
        }
        public int Size
        {
            get { return size; }
            set { this.size = value; }
        }
        public void Add(User user)
        {
            if (data[data.Length - 1] != null)
            {
                data_new = new User[data.Length + 8]; // 如果最后一个元素不为空,说明当前已经存满,长度+8
                for (int j = 0; j < data.Length; j++)
                {
                    data_new[j] = data[j];
                }
                data = data_new;
                capacity = data.Length;
            }
            data[size++] = user;
        }
        public void Remove(int index)
        {
            if (index >= size)
            {
                Console.WriteLine("该元素不存在, 删除失败...");
                return;
            }
            data[index] = null;
            for (int i = index; i < size - 1; i++)
            {
                data[i] = data[i + 1];
            }
            data[size - 1] = null;
            size--;
        }
        public void Update(int index, User user)
        {
            if (index >= size || user == null)
            {
                Console.WriteLine("输入的信息有误...");
                return;
            }
            data[index] = user;
        }
        public User Find(int index)
        {
            return index < size ? data[index] : null;
        }
        public void PrintData()
        {
            foreach (var item in data)
            {
                if (item != null)
                {
                    item.PrintUser();
                }
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace ArrayOperate
{
// User类
    class User
    {
        private string name;
        private int age;
        private string gender;

        public User() { }
        public User(string name, int age, string gender) 
        {
            this.name = name;
            this.age = age;
            this.gender = gender;
        }

        public void PrintUser() 
        {
            Console.WriteLine("姓名:{0}, 性别:{1}, 年龄:{2}", this.Name, this.Gender, this.Age);
        }

        public string Name 
        {
            get { return name; }
            set { this.name = value; }
        }
        public int Age
        {
            get { return age; }
            set { this.age = value; }
        }
        public string Gender
        {
            get { return gender; }
            set { this.gender = value; }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值