static void Test01()
{
ArrayList list = new ArrayList();
//Add方法的参数类型是object类型 在传递参数的时候整型1会转化成object类型 这样属于装箱操作
list.Add(1);
//如果实现 list[0] + 2 得到正确的结果 要将list[0]转化成int类型 也就是说要进行拆箱操作
Console.WriteLine((int)list[0] + 2);
//泛型集合在声明的时候已经确定了里面的元素类型
List<string> list0 = new List<string>();
//里面的传入的数据类型只能和声明时定义的类型保持一致
//泛型能在编译时,提供强大的类型检查,减少数据类型之间的显示转化、装箱操作和运行时的类型检查。
list0.Add("C#编程之道");
list0.Add("C#从入门到精通");
list0.AddRange(list0);
Console.WriteLine(list0.Count);
bool ret = list0.Contains("C#编程之道");
Console.WriteLine(ret);
int index = list0.IndexOf("C#从入门到精通", 2);
Console.WriteLine(index);
List<int> numList = new List<int>();
numList.Add(1);
numList.Add(2);
numList.Add(5);
numList.Add(1);
numList.Add(2);
numList.Add(2);
numList.Add(4);
List<int> newList = numList.GetRange(0, numList.Count);
index = -1;
//记录查找元素的数量
int count = 0;
while (newList.IndexOf(2) != -1)
{
index = newList.IndexOf(2);
count++;
newList.RemoveAt(index);
}
Console.WriteLine(count);
Console.WriteLine("-------------------------");
foreach (var item in numList)
{
Console.WriteLine(item);
}
}
Dictionary:
class Book
{
private string name;
private int price;
private string author;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Price
{
get
{
return price;
}
set
{
price = value;
}
}
public string Author
{
get
{
return author;
}
set
{
author = value;
}
}
public Book(string name, int price, string author)
{
this.Name = name;
this.Price = price;
this.Author = author;
}
public override string ToString()
{
return string.Format("书名:{0} 价格:{1} 作者:{2}", Name, Price, Author);
}
}
static void Test01()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("张杰", "高飞");
dic.Add("刘欢", "好汉歌");
//在一个字典中 键是唯一的
//在一个字典中不同的键可以对应相同的值
dic.Add("周杰伦", "青花瓷");
dic.Add("刘英", "青花瓷");
bool key = dic.ContainsKey("周杰伦");
Console.WriteLine(key);
bool value = dic.ContainsValue("好汉歌");
Console.WriteLine(value);
KeyCollection keys = dic.Keys;
//dic.Remove("刘英");
foreach (var item in keys)
{
Console.WriteLine(item);
}
}
static void Test02()
{
Dictionary<string, Book> books = new Dictionary<string, Book>();
books.Add("0000000",new Book("C#编程之道",56,"王垚"));
books.Add("0000001", new Book("C#从入门到精通", 98, "YY"));
books.Add("0000002", new Book("C#从入门到放弃", 2, "亚东"));
foreach (var item in books)
{
Console.WriteLine(item.Key +" " + item.Value.Price);
}
}
static void Test03()
{
SortedDictionary<string, string> dic = new SortedDictionary<string, string>();
dic.Add("张杰", "高飞");
dic.Add("刘欢", "好汉歌");
//在一个字典中 键是唯一的
//在一个字典中不同的键可以对应相同的值
dic.Add("周杰伦", "青花瓷");
dic.Add("刘英", "青花瓷");
dic.Add("asdef", "青花瓷");
dic.Add("asdeh", "青花瓷");
bool key = dic.ContainsKey("周杰伦");
Console.WriteLine(key);
bool value = dic.ContainsValue("好汉歌");
Console.WriteLine(value);
foreach (KeyValuePair<string,string> item in dic)
{
Console.WriteLine(item.Key);
}
}