本课要点:
1、数组概述
2、一维数组的使用
3、二维数组的使用
4、数组的基本操作
5、数组排序算法
6、ArrayList集合
7、Hashtable类
8、常见错误
一 数组
1 数组引入1
问题:
简单问题:求4个整数的最大值?

int a = 40, b = 60, c = 30, d = 65;
int max = a;
if (b > max) max = b;
if (c > max) max = c;
if (d > max) max = d;
Console.WriteLine(max);
2 数组引入2
问题:
引申问题:求n个整数的最大值?
n的值很大时,仍然采用前面的方法可不可行?

3 数组引入3
如何解决前面遇到的问题?
将n个同类型的变量以整体的形式表示
能够以简单的方式访问整体中的每个元素

二 数组概述
数组是具有相同数据类型的一组数据的集合。数组中的每一个变量称为数组的元素,数组能够容纳元素的数量称为数组的长度。

三 一维数组的使用

1 一维数组的初始化

2 循环输出数组元素

3 常见错误
数组初始值的数目与数组的长度不一样 。

4 判断正误

四 二维数组
1 二维数组的使用

2 二维数组的初始化

3 常见错误1
二维数组的声明语法问题

4 常见错误2
二维数组行数的获取问题

五 数组的基本操作
1 数组的遍历
1)遍历一维数组
int[ ] array = new int[5] { 0, 1 ,2, 3, 4}; // 声明并初始化一维数组
foreach (int i in array ) // 输出一维数组中的所有元素
{
Console.WriteLine(array[i]);
}
2) 遍历二维数组
int[,] arr = new int[2, 3] { { 1, 2, 3 }, { 3, 4, 5 }};//定义二维数组
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(Convert.ToString(arr[i, j]) + "
}
Console.Write("\n");
}
2 添加/删除数组元素

3 对数组进行排序

4 数组的合并与拆分

六 数组的排序算法
1 冒泡排序法

2 直接插入排序法

3 选择排序法

七 ArrayList类

1 ArrayList元素的添加

2 ArrayList元素的删除

3 ArrayList的遍历
问题:
如何循环输出ArrayList集合的元素呢?
ArrayList list = new ArrayList(); //创建一个ArrayList对象
list.Add("TM"); //向ArrayList集合中添加元素
list.Add("C#从入门到精通");
foreach (string str in list) //遍历ArrayList集合中的元素并输出
{
Console.WriteLine(str);
}
八 Hashtable 类

1 Hashtable元素的添加
Add——向Hashtable中添加元素

2 Hashtable元素的删除

3 Hashtable 的遍历
问题:如何循环输出Hashtable集合中的元素呢?
Hashtable hashtable = new Hashtable(); //创建Hashtable对象
hashtable.Add("id", "BH0001"); //向Hashtable中添加元素
hashtable.Add("name", "TM");
hashtable.Add("sex", "男");
Console.WriteLine("\t 键\t 值");
foreach (DictionaryEntry dicEntry in hashtable)
{
Console.WriteLine("\t " + dicEntry.Key + "\t " + dicEntry.Value);
}

4 Hashtable元素的查找
Contains、ContainsKey方法——按键查找

该文章在 2024/10/8 20:56:52 编辑过