- object类是所有类的基类
1、new关键字的作用
1)实例化对象
2)隐藏从父类那里继承过来的同名成员
2、密封类:密封类是不能被继承的 它的关键字是sealed
密封类不能做父类,但是可以做子类
sealed class XiaoSan:Person
{
public void EST()
{
Console.WriteLine(this.Name);
}
}
3、静态类:不能被继承,也就是说静态类不能当父类,必须从对象派生
4、protected关键字:受保护的,只能被该类和它的子类调用,出了这个范围,将不再被允许调用
一、foreach循环
1、语法结构
foreach(变量类型 变量名QiXi in 集合(数组))
{
//循环体
}
执行过程:程序运行到foreach处时,从集合或数组中取出一个元素,赋值给变量QiXi,
然后执行循环体对变量QiXi进行操作
2、foreach与for循环的不同
我们使用foreach循环,只能遍历元素,不能更改元素,如果修改元素的值,只能使用for循环
二、ArrayList
所有元素都是object类型
string double int
1、Add 各种类型 数组、 类
2、AddRange: 添加集合类型
3、Clear()
4、Remove()
5、RemoveAt()
RemoveRange()
6、Reverse()
7、Insert()
InsertRange():
8、Count Capacity
每次集合中实际包含的元素(count)超过了元素的容量(capacity)的时候,集合就会向内存中申请多开辟一倍的空间,保证集合一直够用。
9、Index of()查找括号中的元素第一次出现的位置
10、LastIndexOf()查找括号中的元素最后一次出现的位置
拓:C#中ArrayList 与 string、string[]数组 的转换
1、ArrarList 转换为 string[] :
ArrayList list = new ArrayList();
list.Add("aaa");
list.Add("bbb");
//转换成数组
string[] arrString = (string[])list.ToArray(typeof( string)) ;
2、string[] 转换为 ArrarList :
ArrayList list = new ArrayList(new string[] { "aaa", "bbb" });
3、ArrayList 转换为 string :
ArrayList list = new ArrayList();
list.Add("aaa");
list.Add("bbb");
//转换成数组
string str= string.Join(",", (string[])list.ToArray(typeof( string)));
4、string 转换为 ArrayList :
string str="1,2,3,4,5";
ArrayList b = new ArrayList( str.Split(',') ) ;
https://blog.csdn.net/Aurora_m/article/details/98845209
Hashtable和Dictionary比较:
1、Dictionary是顺序存储,Hashtable则不是。使用foreach,Dictionary是按照Add的顺序排列的,Hashtable则是无序的。
2、单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分。多线程的时候使用HashTable更好。
三、HashTable 键值对集合
查询效率优于ArrayList
1、简单举例 添加键值对
在键值对集合中,我们是根据键去找值的
键值对对象[键]=值;
键值对集合当中,键必须是唯一的,而值可以重复
2、for遍历局限性
3、foreach遍历key值对
4、常用方法:
1)、contains() containsKey() containsValue()
2)、Clear():移除集合中所有元素
练习一、练习ArrayList的上述方法
练习二、例:写一个长度为10的集合,要求在里面随机存放10个数字(0~9),要求所有的数字不重复
#region 练习三、例:写一个长度为10的集合,要求在里面随机存放10个数字(0~9),要求所有的数字不重复
Console.WriteLine("练习三:写一个长度为10的集合,随机存放不重复10个数字(0~9):");
ArrayList arry = new ArrayList(10);
Random r = new Random();
for (int i = 0; i < 10; i++)
{
int rNum = r.Next(0, 10);
if (arry.Contains(rNum))
{
i--;
}
else
{
arry.Add(rNum);
}
}
foreach (int item in arry)
{
Console.Write(item + " ");
}
Console.WriteLine();
#endregion
#region 练习二、练习ArrayList的上述方法
Console.WriteLine("\n\n练习二:练习ArrayList的上述方法");
//创建一个集合
ArrayList Friends = new ArrayList();
//添加元素
Friends.Add("磷叶石"); //string 索引:0
Friends.Add(111); //1
Friends.Add(true); //2
Friends.Add(3.14); //3
//添加集合
string[] arr = { "卡罗尔", "星期二"};
Friends.AddRange(arr);
clear清空集合元素
//girlFriends.Clear();
//移除元素
//girlFriends.Remove("111");
//移除下标所在的元素
//girlFriends.RemoveAt(2); //元素所在的索引
//插入元素
Friends.Insert(3, "安倍晋三"); //在索引3的位置插入了一个元素
string[] Acg = {"路飞", "娜美", "乔巴" };
//插入多个元素
Friends.InsertRange(4, Acg);
//集合中有多少个元素
int len = Friends.Count;
Console.WriteLine("集合中一共有{0}个元素", len);
//集合的容量
int cap = Friends.Capacity;
Console.WriteLine("集合的容量为{0}", cap);
//IndexOf查找括号中的元素第一次出现的位置
int index = Friends.IndexOf("路飞"); //查找
Console.WriteLine("路飞所在的索引为{0}", index);
//LastIndexOf查找括号中的元素最后一次出现的位置
int pose = Friends.LastIndexOf("乔巴");
Console.WriteLine("乔巴所在的索引为{0}", pose);
Console.WriteLine("翻转前集合中现有元素为:");
foreach (object b in Friends)
{
Console.Write(b+" ");
}
Console.WriteLine();
//对元素进行翻转
Friends.Reverse();
Console.WriteLine("翻转后集合中元素为:");
foreach (object b in Friends)
{
Console.Write(b + " ");
}
Console.WriteLine();
#endregion
四、List集合 泛型集合
泛型:定义变量的时候,不指定具体的类型,把参数类型的声明推迟到调用的时候再告诉他。
public static void PrintNumber<T>(T num)
{
Console.WriteLine(num);
}
调用泛型函数:PrintNumber<string>(s); //调用泛型函数
泛型执行效率是最高的,另外,泛型简化了代码量
五、装箱和拆箱
1、装箱:把值类型转换成引用类型
int age=18;
object b=age; //发生了装箱操作
为什么不用ArrayList,就是因为每一次的赋值都会发生装箱操作
2、拆箱:把引用类型转换成值类型
object st=23;
int str=st; //拆箱操作
看两个变量是否发生了装箱和拆箱,要看他们是否存在继承关系
六、Dictionary键值对集合
Dictionary里面的每一个元素都是一个键值对
键必须是唯一的
Dictionary与Hashtable区别:
练习一、将一个数组中的奇数放到一个集合中,再将偶数放到另一个集合中,
最终将两个集合合并为一个集合,并且奇数显示在左边,偶数显示在右边
练习二、统计welcome to china 中每个字符出现的次数,不考虑大小写
#region 练习一:将一个数组中的奇数放到一个集合中,再将偶数放到另一个集合中,最终将两个集合合并为一个集合,并且奇数显示在左边,偶数显示在右边
Console.WriteLine("练习一:使用集合将1, 2, 3, 4, 5, 6的奇数显示在左边,偶数显示在右边:");
int[] JiOu = { 1, 2, 3, 4, 5, 6 };
List<int> JiShu = new List<int>();
List<int> OuShu = new List<int>();
foreach (int item in JiOu)
{
if (item % 2 != 0)
{
JiShu.Add(item);
}
else
{
OuShu.Add(item);
}
}
OuShu.AddRange(JiShu);
foreach (int d in OuShu)
{
Console.Write(d + " ");
}
Console.WriteLine();
#endregion
#region 练习二、统计welcome to china 中每个字符出现的次数,不考虑大小写
Console.WriteLine("\n\n练习二:统计welcome to china 中每个字符出现的次数");
string wtc = "welcome to china";
wtc = wtc.Replace(" ", "");
Dictionary<char, int> characterNum = new Dictionary<char, int>();
foreach (char item in wtc)
{
if (!characterNum.ContainsKey(item))
{
characterNum.Add(item, 1);
}
else
{
characterNum[item] += 1;
}
}
//foreach (char item in characterNum.Keys)
//{
// Console.WriteLine("{0}出现了{1}次", item, characterNum[item]);
//}
foreach (KeyValuePair<char, int> item in characterNum)
{
Console.WriteLine("{0}出现了{1}次", item.Key, item.Value);
}
#endregion