集合命名空间:
using system.collections. 非泛型集合
using system.collections.Generic. 泛型集合
为什么要用集合:
1、数组一旦声明长度就固定了。
2、集合有很多方法可以用
等
常用集合:
类似数组集合:ArrayList List<>
键值对集合:Hashtable Dictionary<K V>
栈集合:Stack
队列:Queye
等
ArrayList:
class Program
{
static void Main(string[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.Add(); //增加一个元素
arrayList.AddRange(new int[] { , });//增加一个集合
arrayList.Insert(, "开始:");//在指定位子插入一个元素
arrayList.InsertRange(, new string[] { "一", "二", "三" });//指定位置插入集合
arrayList.RemoveAt();
arrayList.Clear();//清空集合
for (int i = ; i < arrayList.Count; i++)
{
Console.WriteLine(arrayList[i]);
}
}
}
Remove()不是根据对象来判断的,而是根据值来判断。
Contains()是否包含某个值,也是根据值来判断的。
集合转为数组:.ToArray().
排序: 1、.sort() 升序 没有降序 可以用.Reverse()颠倒位置实现。
2、想让任何集合实现排序需要实现 IComparable接口 。
3、直接调用sort是使用ICompareble 接口的默认方式来排序。可以用sort的重载,使用自己的比较器。
Hashtable:
简单添加和获取:
class Program
{
static void Main(string[] args)
{
Hashtable hashtable = new Hashtable();
hashtable.Add("Sam", "Sam");
hashtable.Add("Penny", new Person() { Name = "Penny" });
Console.WriteLine(hashtable["Sam"]);
Person Penny = hashtable["Penny"] as Person;
Console.WriteLine(Penny.Name); }
}
键值对集合的键不能重复。
判断是否存在某个键:.ContentsKey() 是否存在某个值:.ContentsValue()
遍历Hash table:
class Program
{
static void Main(string[] args)
{
Hashtable hashtable = new Hashtable();
hashtable.Add("Sam", "Sam");
hashtable.Add("Penny", "Penny");
//遍历键
foreach (object item in hashtable.Keys)
{
Console.WriteLine("Key:{0}----Value:{1}",item,hashtable[item]);
}
//遍历值
foreach (object item in hashtable.Values)
{
Console.WriteLine("Value:{0}",item);
}
//遍历键值对
foreach (DictionaryEntry item in hashtable)
{
Console.WriteLine("Key:{0}---Value{1}",item.Key,item.Value);
} }
}
集合小练习:
两个ArrryList集合的并集。
class Program
{
static void Main(string[] args)
{
ArrayList A = new ArrayList(new string[] {"a","b"});
ArrayList B = new ArrayList(new string[] { "a", "b","c","d" });
for (int i = ; i <B.Count; i++)
{
if (!A.Contains(B[i]))
{
A.Add(B[i]);
}
}
for (int i = ; i < A.Count; i++)
{
Console.WriteLine(A[i]);
}
}
}
随机生成十个1-100之间的数放到arraylist中,这些数必须是偶数且不能重复。
class Program
{
static void Main(string[] args)
{
ArrayList arrayList = new ArrayList();
Random random = new Random();
while (arrayList.Count<)
{
int r = random.Next(, );
if (!arrayList.Contains(r)&&((r%)==))
{
arrayList.Add(r);
}
}
for (int i = ; i < arrayList.Count; i++)
{
Console.WriteLine(arrayList[i]);
}
}
}
上面程序把random放循环外面效率更好,因为无参的random构造函数以系统时间为种子,而一遍循环完了以后时间还没来得及变,就会生成相同的数。
泛型集合:
List<string> list = new List<string> ()
和 arraylist相比:
数据类型固定,有更多的方法可以用。
Dictionary<string, int> dic = new Dictionary<string, int> ;
和hashtable相比:
键和值都有类型约束。
遍历键值对:
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("Sam", );
dic.Add("Penny", );
foreach (KeyValuePair<string,int> item in dic)
{
Console.WriteLine("keys:{0}--value:{1}",item.Key,item.Value);
}
}
}
判断字符串中每个字母出现的次数:
class Program
{
static void Main(string[] args)
{
string str = "wellcome to china";
Dictionary<char, int> dict = new Dictionary<char, int>();
for (int i = ; i < str.Length; i++)
{
if (char.IsLetter(str[i]))
{
if (dict.ContainsKey(str[i]))
{
dict[str[i]]++;
}
else
{
dict.Add(str[i], );
}
}
}
foreach (KeyValuePair<char,int> item in dict)
{
Console.WriteLine("{0}----{1}",item.Key,item.Value);
}
}
}