public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public int Height { get; set; }
public int Weight { get; set; }
}
#region ArrayList集合
public void TestArrayList()
{
//存储object类型做数据,不安全类型
//需要不断地装箱和拆箱,性能损耗严重
//装箱:将int或者string等不同类型变量通过隐式转换赋给object对象
//拆箱:将object对象通过显示转换赋给int或者string等不同类型的变量
ArrayList myArrayList = new ArrayList();
//添加int类型的元素
myArrayList.Add(123);
//添加string类型的元素
myArrayList.Add("abc");
//在指定索引处添加元素
myArrayList.Insert(1, 123 + "abc");
//在指定索引处移除元素
myArrayList.RemoveAt(0);
//移除指定元素
myArrayList.Remove("abc");
//清空ArrayList集合
myArrayList.Clear();
//复制ArrayList集合副本
myArrayList.Clone();
}
#endregion
#region 泛型:List集合
public void TestList()
{
//实例化泛型为int类型的List集合
List<int> intList = new List<int>();
//添加int类型的元素
intList.Add(1);
//修改指定索引处元素
intList[0] = 2;
//在指定索引处添加元素
intList.Insert(0, 3);
//在指定索引处移除元素
intList.RemoveAt(0);
//移除指定元素,如果有多处一样的元素,只删除第一个匹配上的元素
intList.Remove(2);
//清空List集合
intList.Clear();
//添加自定义类型的元素
List<Person> personList = new List<Person>();
//添加第一个人
Person thirdPerson = new Person();
thirdPerson.Name = "王五";
thirdPerson.Age = 20;
thirdPerson.Height = 178;
thirdPerson.Weight = 73;
personList.Add(thirdPerson);
//添加第二个人
Person fourthPerson = new Person()
{
Name = "赵六",
Age = 19,
Height = 180,
Weight = 74
};
personList.Add(fourthPerson);
//添加第三个人
Person fifthPerson = new Person()
{
Name = "田七",
Age = 18,
Height = 182,
Weight = 76
};
personList.Add(new Person() {
Name = "田七",
Age = 18,
Height = 182,
Weight = 76
});
//取值:获取这3个人的信息
Person thirdPerson2 = personList[0];
Person fourthPerson2 = personList[1];
Person fifthPerson2 = personList[2];
//删除指定索引位置的自定义对象
personList.RemoveAt(0);
// 删除匹配的自定义对象
personList.Remove(thirdPerson);
personList.Remove(fourthPerson);
//虽然第三个人的信息(由fifthPerson变量接收)和直接实例化的person类的信息一致(均为田七的个人信息),
//但是只要重新实例化对象(new了一个person类)了,系统就会重新开辟新的内存空间
//由于fifthPerson变量并没有直接被添加到personList中,所以直接删除fifthPerson并不会成功,但也不会报错
personList.Remove(fifthPerson);
//要想删除掉没有定义变量接收的第三人信息(直接实例化的Person类),可以先定义一个变量接收第三人的信息,然后再删除掉
personList.Remove(fifthPerson2);
}
#endregion
#region Dictionary 字典集合
public void TestDictionary()
{
//Dictionary 字典集合在定义时要指定它的键和值的类型
Dictionary<int, string> myDictionary = new Dictionary<int, string>();
//添加方法一:
//通过Add方法添加键值对进行赋值,键一定是唯一的,值可以不唯一,如果重复添加相同的键会报错
myDictionary.Add(1, "98分");
myDictionary.Add(2, "92分");
myDictionary.Add(3, "89分");
//已经存在键为1的数据了,故系统会报错
myDictionary.Add(1, "88分");
//添加方法二:
//通过找到指定键值来赋值,注意:中括号里不是索引值,而是键值
//如果已存在键值为1的数据,则进行修改,否则进行添加
myDictionary[1] = "88分";
//此时不存在键值为4的数据,故进行数据添加,系统不会报错
myDictionary[4] = "99分";
//添加方法三:
Dictionary<int, string> myDictionary1 = new Dictionary<int, string>()
{
{ 1, "98分" },
{ 2, "92分" },
{ 3, "89分" },
{ 4, "99分" }
};
Dictionary<string, string> myDictionary2 = new Dictionary<string, string>()
{
{ "A", "5 Stars" },
{ "B", "4 Stars" },
{ "C", "3 Stars" },
{ "D", "2 Stars" },
{ "E", "1 Star" }
};
//通过key值取出字典集合里的value值,接收类型取决于定义时的value类型
string currentStars = myDictionary2["A"];
bool bRemoved = myDictionary2.Remove("A");
if (bRemoved)
{
// show removed successfully!
}
}
#endregion