using System;
using System.Collections.Generic;
using System.Text;
namespace ArrayOperate
{
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];
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
{
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; }
}
}
}