1)定义:
集合:
某些特定的对象集中在一起就是集合
数组:
可以存储相同数据类型的一堆数据的容器
(2)集合
集合分为泛型集合和非泛型集合
泛型集合分为单列和双列集合 (List<T>和Dictionary<K,V>)
非泛型集合分为单列和双列集合 (ArrayList和HashTable)
(3)常用的方法和属性
Add(); //添加
Remove(); //删除
RemobeAt(); //根据索引删除 不适用 双列集合
count //集合存储元素的个数
Contains(); //检测元素是否存在
ContainsKey(); //
ContainsValue();
Capcity //集合占用空间
注意事项:如果删除了集合中的某一个元素,那么集合的索引会自动维护
(4)遍历方案:
ArrayList list=new ArrayLIst();
Student stu1=new Student();
stu1.Name="jiejie";
stu1.Age=15;
list.Add(stu1);
Student stu2=new Student();
stu2.Name="jiji";
stu2.Age=18;
list.Add(stu2);
//foreachforeach(Student item in list)
{
Console.WriteLine(item.Age+"\t"+item.Name);
}
//forfor(int i=0;i<list.Count;i++)
{
Console.WriteLine((Student)list[i].Name);
}
HashTable 遍历三种方案
HashTable table=new HashTable();
第一种方式:遍历所有的Keysforeach(var item in table.Keys)
{
//一个item代表一个key
Console.WriteLine("key是{0}\tvalue是{1}",item,table[item]);
}
第二种遍历方式:遍历所有的value集合foreach(var item in table.Values)
{
//一个item代表一个value
Console.WriteLine("value是{0}",item);
}
//遍历整个tableforeach(DictionaryEntry item in table)
{
Console.WriteLine("key是{0}\tvalue是{1}",item.Key,item.Value);
}
Copyright © 2004-2024 Ynicp.com 版权所有 法律顾问:建纬(昆明)律师事务所 昆明市网翼通科技有限公司 滇ICP备08002592号-4