集合
1、集合与数组的比较
2、ArrayList集合
3、Hashtable集合
4、泛型集合
5、字典集合
1 集合与数组的比较
**数组:**数组存储的对象的数据类型必须一致,且数组的大小是不能改变的,数组的长度用length.
**集合:**类型随便,长度是可以任意改变的,集合的长度用count。
2 ArrayList集合
添加数据容易,拿出元素难
常用的方法
//创建集合对象
ArrayList list=new ArrayList();
//添加单个数据
list.Add(1);
list.Add("张");
list.Add('男');
//添加一组数据
list.AddRange(new int[]{1,2,3});//添加一个数组
list.AddRange(list);//添加自己
//删除元素
//清空所有元素
list.Clear();
//删除单个元素
list.Remove('男');
//根据下标删除元素
list.RemoveAt(1);
//根据下标删除一定范围的元素
list.RemoveRange(0,1);
//元素的反转
list.Reverse();
//在指定的位置插入一个元素
list.Insert(1,10);
//在指定的位置插入一个集合
list.InsertRange(1,new string[]{"zhang","shao"});
//判断元素是否包含,返回的是一个bool类型 的值
bool b=list.Contains("zhang");
//用for循环遍历
for(int i=0;i<list.length,i++)
{
Console.WriteLine(list[i]);
}
ArrayList集合长度的问题
count——表示集合中实际包含的元素个数
capcity——表示集合中可以包含的元素个数
每次集合中实际包含的元素个数(count)超过了可以包含的元素的个数(capcity)的时候,集合就会向内存中申请多开辟一倍的空间,来保证集合的长度一直够用。
Hashtable集合
又称键值对集合。其中键必须是唯一的。
//创建集合对象
Hashtable ht=new Hashtable();
//添加数据
ht.Add(1,"zhang");
ht.Add(2,"shao");
ht.Add(3,true);
ht[1]="lei";//这种方法可以覆盖掉原先1键所赋的值。
//清空集合中的所有元素
ht.Clear();
//清除指定键所对应的值
ht.Remove(1);
//不能用for循环来遍历,可以用foreach循环来遍历
//其中item表示集合中的元素,ht表示集合
//var是一种对变量不需要明确的定义的类型。
foreach(var item in ht)
{
Console.WriteLine(ht[item]);
}
泛型集合
类似于ArrayList集合。确定了集合的类型,集合中元素的类型也就确定了。相比较ArrayList集合,泛型集合不需要手动的添加命名空间。
//创建集合对象
List<int> list=new List<>();
//添加元素
//添加单个元素
list.Add(1);
list.Add("zhang");
list.Add('男');
//添加集合
list.AddRange(new int[]{1,2,3});
//可以转换成为数组,调用.ToArray();
int[] nums=list.ToArray();
//数组也可以转换成为集合,调用ToList();
char[] chs=new char[]{'男','女'};
list<char> listc=chs.ToList();
字典集合
类似于Hashtable集合。
//创建集合对象,需要知道键的类型和值的类型dicAdd
Dictionary<int,string> dic=new Dictionary<int,string>();
//添加数据
dic.Add(1,"zhang");//必须和Dinctionary后面的<>里面的类型一致.
//一对一对的遍历
foreach(KeyValuePair kv in dic)
{
Console.WriteLine("{0}==={1}",kv.Key,kv.Value);
}
集合的练习
ArrayList的练习
//下一个长度为10的集合,要求在里面随机存放10个数字(1-9)
//但要求所有的数字不重复
ArrayList list = new ArrayList();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
int rNumber = r.Next(0, 10);
//集合中没有随机数
if (!list.Contains(rNumber))
{
list.Add(rNumber);
}
else
{
//集合中有随机数
//一旦产生了重复的随机数,这次循环就不算数
i--;
}
}
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
Console.ReadKey();
泛型集合的练习
//将一个数组中的奇数放到一个集合中,再将偶数放到另一个集合中
//最终将两个集合合并为一个集合,并且奇数显示在左边 偶数显示在右边。
int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> listOu = new List<int>();
List<int> listJi = new List<int>();
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] % 2 == 0)
{
listOu.Add(nums[i]);
}
else
{
listJi.Add(nums[i]);
}
}
listJi.AddRange(listOu);
foreach (int item in listJi)
{
Console.WriteLine(item+" ");
}
Console.ReadKey();
字典集合练习:
//统计 Welcome to china中每个字符出现的次数 不考虑大小写
string str = "Welcome to china";
//字符------出现的次数
//键------值
Dictionary<char, int> dic = new Dictionary<char, int>();
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ' ')
{
continue;
}
//如果dic已经包含了当前循环到的这个键
if (dic.ContainsKey(str[i]))
{
//值再次加1
dic[str[i]]++;
}
else//这个字符在集合中是第一次出现
{
dic[str[i]] = 1;
}
}
foreach (KeyValuePair<char,int> kv in dic)
{
Console.WriteLine("字母{0}出现了{1}次",kv.Key,kv.Value);
}
Console.ReadKey();