本課要點(diǎn):
1、數(shù)組概述
2、一維數(shù)組的使用
3、二維數(shù)組的使用
4、數(shù)組的基本操作
5、數(shù)組排序算法
6、ArrayList集合
7、Hashtable類
8、常見錯誤
一 數(shù)組
1 數(shù)組引入1
問題:
簡單問題:求4個(gè)整數(shù)的最大值?

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 數(shù)組引入2
問題:
引申問題:求n個(gè)整數(shù)的最大值?
n的值很大時(shí),仍然采用前面的方法可不可行?

3 數(shù)組引入3
如何解決前面遇到的問題?
將n個(gè)同類型的變量以整體的形式表示
能夠以簡單的方式訪問整體中的每個(gè)元素

二 數(shù)組概述
數(shù)組是具有相同數(shù)據(jù)類型的一組數(shù)據(jù)的集合。數(shù)組中的每一個(gè)變量稱為數(shù)組的元素,數(shù)組能夠容納元素的數(shù)量稱為數(shù)組的長度。

三 一維數(shù)組的使用

1 一維數(shù)組的初始化

2 循環(huán)輸出數(shù)組元素

3 常見錯誤
數(shù)組初始值的數(shù)目與數(shù)組的長度不一樣 。

4 判斷正誤

四 二維數(shù)組
1 二維數(shù)組的使用

2 二維數(shù)組的初始化

3 常見錯誤1
二維數(shù)組的聲明語法問題

4 常見錯誤2
二維數(shù)組行數(shù)的獲取問題

五 數(shù)組的基本操作
1 數(shù)組的遍歷
1)遍歷一維數(shù)組
int[ ] array = new int[5] { 0, 1 ,2, 3, 4}; // 聲明并初始化一維數(shù)組
foreach (int i in array ) // 輸出一維數(shù)組中的所有元素
{
Console.WriteLine(array[i]);
}
2) 遍歷二維數(shù)組
int[,] arr = new int[2, 3] { { 1, 2, 3 }, { 3, 4, 5 }};//定義二維數(shù)組
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 添加/刪除數(shù)組元素

3 對數(shù)組進(jìn)行排序

4 數(shù)組的合并與拆分

六 數(shù)組的排序算法
1 冒泡排序法

2 直接插入排序法

3 選擇排序法

七 ArrayList類

1 ArrayList元素的添加

2 ArrayList元素的刪除

3 ArrayList的遍歷
問題:
如何循環(huán)輸出ArrayList集合的元素呢?
ArrayList list = new ArrayList(); //創(chuàng)建一個(gè)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 的遍歷
問題:如何循環(huán)輸出Hashtable集合中的元素呢?
Hashtable hashtable = new Hashtable(); //創(chuàng)建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 編輯過