学习C#的第五天
-
查询数组元素
- IndexOf - LastIndexOf - BinarySearch - 二分法, 用于查找元素首次出现的索引值
//Array.BinarySearch 适用于已排好序的数组, 不然容易无法正常查询
int[] myArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int result = Array.BinarySearch(myArray, 13);
//非标准示范
int[] myArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6 };
int result2 = Array.BinarySearch(myArray, 13); // 查询结果-20, 正确应输出12
- Contains()
int[] myArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6 };
bool myb = ((System.Collections.IList)myArray).Contains(8);
if (myb) Console.WriteLine("存在该元素");
else Console.WriteLine("不存在");
- 数组排序
- 升序 Sort
- 降序 Reverse - 数组合并与拆分
int[] num = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] num2 = new int[10] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
//合并
int[] fnum = new int[20];
//数组1 数组2 长度
Array.Copy(num, fnum, 10);
//数组1 指定索引 数组2 指定索引 长度
Array.Copy(num2, 0, fnum, 10, 10);
Console.Write("两个数组合并");
foreach (int k in fnum) {
if (k % 10 == 0 ) Console.WriteLine();
Console.Write(k + "\t");
}
//拆分
int[] fnum2 = new int[7];
Array.Copy(fnum, 11, fnum2, 0, 7);
foreach (int a in fnum2) Console.Write(a + "\t");
-
ArrayList类
- ArrayList类的属性- 元素添加 - Add - AddRange
ArrayList myal = new ArrayList(5);
//多次添加一个元素
myal.Add(123);
myal.Add("mystring");
myal.Add('a');
myal.Add(25.6);
myal.Add(10L);
//一次添加多个元素
string[] sa = { "张三", "李四", "王五", "赵六"};
myal.AddRange(sa);
//遍历
foreach (object elem in myal) Console.Write(elem + "\t");
- 元素删除
- Remove
- RemoveAt(索引值)
- RemoveRange(起始索引, 删除个数)
- Clear();
myal.Remove("张三");
myal.RemoveAt(5);
myal.RemoveRange(5, 2);
myal.Clear();
- ArrayList类的遍历与查找
//遍历
foreach (object elem in myal) Console.Write(elem + "\t");
//查找
//IndexOf && LastIndexOf
Console.WriteLine(myal.IndexOf("张三"));
Console.WriteLine(myal.LastIndexOf("张三"));
//BinarySearch 二分法会把元素转换成二进制,非数值类型可能会出错
ArrayList newal = new ArrayList();
int[] num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
newal.AddRange(num);
newal.Add(10);
newal.Add(20);
Console.WriteLine(newal.BinarySearch(20));
-
方法
- 参数数组 params
//不必传递数组,而是传递同类型的几个参数
static int Sum(params int[] num)
{
int sum = 0;
foreach (int i in num) sum += i;
return sum;
}
- 引用参数 ref
- 调用函数变量必须为非常量
- 调用变量必须初始化
static void Square(ref int num)
{
int a = num;
num *= num;
Console.WriteLine("{0}的平方是{1}", a, num);
}
static void Main(string[] args) {
int num = 10;
Square(ref num);
//值传递:传值给调用方法,方法中的变量不会影响参数列表, 除非使用引用参数
Console.WriteLine(num);
Console.ReadKey();
}
- 输出参数 out
- 希望返回多个值
static void Main(string[] args) {
int[] intArray = { 12, 3, 5, 2, 5, 548, 21, 8, 54, 21 };
int MaxIndex;
Console.WriteLine("数组最大值是{0}, 最大值索引是{1}", MaxNum(intArray, out MaxIndex), MaxIndex);
Console.ReadKey();
}
static int MaxNum(int[] num, out int MaxIndex)
{
int Maxnum = num[0];
MaxIndex = 0;
for (int i=0; i<num.Length; i++)
{
if (Maxnum < num[i]) {
Maxnum = num[i];
MaxIndex = i;
}
}
return Maxnum;
}
- 静态方法 和 实例方法
- 无法在静态方法中调用实例类的成员
- 实例方法中可以调用任何成员
- 调用实例方法时,必须使用类的实例或者对象来引用
Porgram p = new Program();
p.exampleMethod();
- 虚方法
- 一个有virtual修饰符的实例方法
- 用于引用新方法
- 重写方法
- 使用现有的继承方法去专业化
- 不想让其他类继承并重写方法,可以使用sealed 保护方法
class class2 : class1 //class2继承class1
{
public override sealed void Write()
{
Console.WriteLine("这是一个重写方法,被称为已重写的基方法");
}
}
// class3 无法继承 class2
class class3 : class2
{
public override void Write()
{
Console.WriteLine("这是再次重写的方法");
}
- 外部方法
- 外部方法在外部实现,通常是C#以外的语言
- 外部方法不可以是泛型
namespace test3
{
class Program
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);
static int Main(string[] args)
{
Console.WriteLine("请输入你的姓名:");
string name = Console.ReadLine();
//利用return 弹出对话框
return MessageBox(0, "你好! " + name + "\n\n" + "欢迎来到我要自学网", "欢迎提示", 0);
}
}
}
- 分布方法 partial
namespace test3
{
public partial class Program
{
//声明 定义 一个分布类
partial void Wirte();
//声明分布方法 默认私有
partial void Wirte()
{
Console.WriteLine("这是一个分布方法");
}
}
public partial class Program
{
static void Main(string[] args)
{
//调用分布方法
Program p = new Program();
p.Wirte();
Console.ReadKey();
}
}
}