public class Tools{
public string Name{get ;set;}
}
#region 0.1ArrayList集合
////告诉内存,我要存储内容
//ArrayList list = new ArrayList();
////方法 Add方法
//list.Add("李小龙");
//list.Add("成龙");
//list.Add("甄子丹");
////0.1打印出每个人的名称
//foreach (Object item in list)
//{
// Console.WriteLine("姓名为:~~~~~~~~~~~~~~~~~~" + item + "~~~~~~~~~~~~~~~~~");
//}
////0.2单独获取信息
//Console.WriteLine("单独获取信息:" + list[1]);
////0.3删除
//list.Remove("李小龙");
//Console.WriteLine("_______________删除后__________________");
//foreach (Object item in list)
//{
// Console.WriteLine("姓名为:~~~~~~~~~~~~~~~~~~" + item + "~~~~~~~~~~~~~~~~~");
//}
////0.4添加
//Console.WriteLine("添加");
//list.Add("李小龙");
//Console.WriteLine("!!!!!!!!!!!!!!添加!!!!!!!!!!!!!!!!!!!!!");
//foreach (Object item in list)
//{
// Console.WriteLine("姓名为:~~~~~~~~~~~~~~~~~~" + item + "~~~~~~~~~~~~~~~~~");
//}
////0.5添加 Add向后追加
//Console.WriteLine("向后追加");
//list.Insert(1, "李谦");
//Console.WriteLine("!!!!!!!!!!!!!!向后追加!!!!!!!!!!!!!!!!!!!!!");
//foreach (Object item in list)
//{
// Console.WriteLine("姓名为:~~~~~~~~~~~~~~~~~~" + item + "~~~~~~~~~~~~~~~~~");
//}
////0.6判断李小龙是否在名称为list中
//bool flag = list.Contains("李小龙");
//Console.WriteLine(flag);
////修改 "李谦" 为 "刘谦"
////数组中元素个数用XX.Length 集合元素用XX.count
//for (int i = 0; i < list.Count; i++)
//{
// if (list.Contains("李谦"))
// {
// //存在 就修改
// if (list[i].Equals("李谦"))
// {
// list[i] = "刘谦";
// break;
// }
// }
// else
// {
// Console.WriteLine("没有该人,如何修改!!");
// }
//}
//Console.WriteLine("!!!!!!!!!!!!!!修改后!!!!!!!!!!!!!!!!!!!!!");
//foreach (Object item in list)
//{
// Console.WriteLine("姓名为:~~~~~~~~~~~~~~~~~~" + item + "~~~~~~~~~~~~~~~~~");
//}
//方法二
// //ArrayList list = new ArrayList();
// Tools p1 = new Tools();
// p1.Name = "六道神君";
// Tools p2 = new Tools(); ;
// p2.Name = "天使神茫";
// Tools p3 = new Tools();
// p3.Name = "魔君";
// list.Add(p1);
// list.Add(p2);
// list.Add(p3);
// Tools p4 = new Tools();
// p4.Name = "邪神";
// // list.Add(p4); //按顺序排列
// list.Insert(0, p4);//下标定到0
// //遍历集合中所有的元素
// foreach (Tools person in list)
// {
// Console.WriteLine(person.Name);
// }
// Console.WriteLine("-------------------------------删除后--------------------------");
//// 判断所
// if (list.Contains(p4))
// {
// list.RemoveAt(0);//下标
// // list.Remove(p4);//实际存在
// }
// else
// {
// Console.WriteLine("-------------数据不存在-----------");
// }
// foreach (Tools person in list)
// {
// Console.WriteLine(person.Name);
// }
Hashtable table=new Hashtable();
Person p1=new Person();
p1.Name = "李连杰";
Person p2 = new Person();
p2.Name = "巩俐";
Person p3 = new Person();
p3.Name = "张靓颖";
table.Add("001", p1);
table.Add("002", p2);
table.Add("003", p3);
//遍历HashTable三种方案
/*
* 无论是java还是C#,引用类型对象都会自动调用它的ToString()
ToString()命名空间.类名
*/
//方式一:通过key获取key和value
foreach (string key in table.Keys)
{
//通过key获取value
Console.WriteLine("key是{0} value是{1}", key, ((Person) table[key]).Name);
}
Console.WriteLine("===========================");
//方式二:只获取value
foreach (Object value in table.Values)
{
Console.WriteLine(((Person)value).Name);
}
Console.WriteLine("===================================");
//方式三:key和value同时遍历 key+value
foreach (DictionaryEntry item in table)
{
Console.WriteLine("key是{0} value是{1}",item.Key,((Person)item.Value).Name);
}
//删除 Remove()
// table.Remove("001");
//修改 咱们修改“002” key对应value值得name属性为“大美丽”
//先判断 是否存在某个key
if (table.ContainsKey("002")) //存在该key
{
Person p = (Person) table["002"];
p.Name = "大美丽";
}
Console.WriteLine("==========修改后==========");
//方式三:key和value同时遍历 key+value
foreach (DictionaryEntry item in table)
{
Console.WriteLine("key是{0} value是{1}", item.Key, ((Person)item.Value).Name);
}
//Console.WriteLine("==========删除后==========");
////方式三:key和value同时遍历 key+value
//foreach (DictionaryEntry item in table)
//{
// Console.WriteLine("key是{0} value是{1}", item.Key, ((Person)item.Value).Name);
//}
Console.ReadLine();
#endregion