|
单值序列
双值系列
存入数据
遍历,重点,3种遍历
|
//ICollection <-- IList <-- ArrayList
//单值序列
System.Collections.IList list1 = new System.Collections.ArrayList();
//存入
list1.Add("1234");//string对象
list1.Add(1234);//Int32对象
//提取
string s1 = (string)list1[0];
Int32 ii = (Int32)list1[1];
Response.Write(s1 + "<br>" + ii);
//混合遍历
System.Collections.IList list2 = new System.Collections.ArrayList();
list2.Add(new Cat());
list2.Add(new Dog());
list2.Add(new Dog());
list2.Add(new Cat());
list2.Add(new Dog());
//利用接口实现混合遍历
//////for (int i = 0; i < list2.Count; i++)
//////{
////// Response.Write(((Pet)list2[i]).Show());
//////}
for (int i = 0; i < list2.Count; i++)
{
object o = list2[i];
if(o is Cat) //C# is 就是JAVA instanceof 运算符 +-*/
{
Response.Write( ((Cat)o).Show()) ;
}
if (o is Dog)
{
Response.Write(((Dog)o).Show());
}
}
//双值系列
//行 -- 335 (键) 解释 (值)
System.Collections.IDictionary dic = new System.Collections.Hashtable();//哈希表
//存入数据
//dic.Add("键", "值");//注意键不能重复,且无序,值可以重复
dic.Add("北京", "中国的大城市");
dic.Add("华盛顿", "美国的首都");
dic.Add("香港", "中国的大城市");
dic.Add("上海", "中国的大城市");
//取数据,根据键取值
//上海是怎么回事
Response.Write("<br>" + dic["上海"]+"<br>");
Response.Write("<br>");
//遍历,重点,3种遍历
//1,键的遍历
System.Collections.ICollection keys = dic.Keys;
System.Collections.IEnumerator ie = keys.GetEnumerator();//枚举,
while (ie.MoveNext())
{
Response.Write(ie.Current.ToString());
}
Response.Write("<br>");
//2,值的遍历
System.Collections.ICollection values = dic.Values;
ie = values.GetEnumerator();
while (ie.MoveNext())
{
Response.Write(ie.Current);
}
Response.Write("<br>");
//3,键与值共同遍历
keys = dic.Keys;
ie = keys.GetEnumerator();
while (ie.MoveNext())
{
object key = ie.Current;
Response.Write(string.Format("键={0},值={1}", key, dic[key])+"<br>");
}
Response.Write("<br>");
//非泛型的问题:数据类型混乱。
dic.Add("abcd", "xyz");
dic.Add(1234, "dsaf");
dic.Add("dsaf", 222);
dic.Add("asdf", System.DateTime.Now);
|
website3 |
枚举实例 |
enum MyColor
{
red,
pink,
yellow,
black,
green,
blue
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyColor c1 = MyColor.pink;
Response.Write(c1);
Response.Write((int)c1);
if (c1 == MyColor.pink)
{
Response.Write("粉色");
}
}
}
|
website4 |
栈与队列
//队列--先进先出System.Collections.Queue
//入队:q.Enqueue("第一个人");
//出队:q.Dequeue();
//堆栈--先进后出System.Collections.Stack
//入栈
s.Push("第一颗子弹");
出栈:s.Pop()
int n = q.Count;
动态的注意:q.Count;的值
|
//栈与队列
//队列--先进先出
System.Collections.Queue q = new System.Collections.Queue();
//入队
q.Enqueue("第一个人");
q.Enqueue("第二个人");
q.Enqueue("第3个人");
q.Enqueue("第4个人");
//出队
Response.Write("共:"+q.Count+"人,依次出队列:");
int n = q.Count;
for (int i = 0; i < n; i++)
{
Response.Write(q.Dequeue()+">>");
}
Response.Write("<br>");
//堆栈--先进后出
System.Collections.Stack s = new System.Collections.Stack();
//入栈
s.Push("第一颗子弹");
s.Push("第2颗子弹");
s.Push("第3颗子弹");
s.Push("第4颗子弹");
//出栈
Response.Write("共:" + s.Count + "颗子弹,依次射击:");
n = s.Count;
for (int i = 0; i <n; i++)
{
Response.Write(s.Pop()+">>");
}
|
website5 |
泛型
//泛型Dictionary在对象上的使用。
IDictionary<int, PersonInfo> dic2 = new Dictionary<int, PersonInfo>();
//判断某个 键 是否存在:dic2.ContainsKey(1002),//重点:dic2.TryGetValue(1002, out px)
//清空
//dic2.Clear();
//3,键值共同遍历
foreach (int x in dic2.Keys)
{
Response.Write(string.Format("键={0},值={1}{2}", x, dic2[x],"<br>"));
}
|
//泛型
//单值
System.Collections.Generic.IList<string> list1 = new System.Collections.Generic.List<string>();
list1.Add("1234");
//list1.Add(1234);
//双值
System.Collections.Generic.IDictionary<int, string> dic = new System.Collections.Generic.Dictionary<int, string>();
dic.Add(1001, "adsf");
//dic.Add("dfs", 123);
//泛型Dictionary在对象上的使用。
IDictionary<int, PersonInfo> dic2 = new Dictionary<int, PersonInfo>();
//添加
PersonInfo p1=new PersonInfo(1001,"mike",19);
dic2.Add(p1.Id, p1);
PersonInfo p2 = new PersonInfo(1002, "tom", 19);
dic2.Add(p2.Id, p2);
PersonInfo p3 = new PersonInfo(1003, "jerry", 19);
dic2.Add(p3.Id, p3);
//取值--根据键取值
//取1003
Response.Write("1003号是:" + dic2[1003]);
Response.Write("<br>");
//判断某个 键 是否存在
//判断1002 有没有,有打印,没有报错
//if (dic2.ContainsKey(1002))
//{
// Response.Write("1002号是:" + dic2[1002]);
//}
//else
//{
// Response.Write("没有此人");
//}
//重点
PersonInfo px = null;
if (dic2.TryGetValue(1002, out px))
{
Response.Write(px);
}
else
{
Response.Write("查无此人");
}
Response.Write("<br>");
//清空
//dic2.Clear();
//遍历
//1, 键 的遍历
foreach (int x in dic2.Keys)
{
Response.Write(x+" ," );
}
Response.Write("<br>");
//2,值的遍历
foreach (PersonInfo s in dic2.Values)
{
Response.Write(s);
}
Response.Write("<br>");
//3,键值共同遍历
foreach (int x in dic2.Keys)
{
Response.Write(string.Format("键={0},值={1}{2}", x, dic2[x],"<br>"));
}
|
袁博 |
数据词典
判断一句话中每个单词的个数
|
方法一(适合复杂的)
//CountInfo count=null;
//IDictionary<string, CountInfo> dic1 = new Dictionary<string, CountInfo>();
//string[] strs = TextBox1.Text.Split(' ');
//for (int i = 0; i < strs.Length; i++)
//{
// if (dic1.TryGetValue(strs[i],out count))
// {
// count.Num = count.Num + 1;
// }
// else
// {
// count = new CountInfo(strs[i], 1);
// dic1.Add(strs[i], count);
// }
//}
//foreach (string key in dic1.Keys)
//{
// Response.Write(string.Format("这个单词{0}, 共{1}<br>",key,dic1[key]));
//}
方法二(简练)
int count = 0;
IDictionary<string, int> dic1 = new Dictionary<string, int>();
string[] strs = "apler blue hit hit blue".Split(' ');
for (int i = 0; i < strs.Length; i++)
{
if (dic1.TryGetValue(strs[i], out count))
{
dic1[strs[i]] = count + 1;
}
else
{
dic1.Add(strs[i], 1);
}
}
foreach (string key in dic1.Keys)
{
Response.Write(string.Format("这个单词{0}, 共{1}<br>", key, dic1[key]));
}
|