C# 數(shù)組5完整版課件_第1頁
C# 數(shù)組5完整版課件_第2頁
C# 數(shù)組5完整版課件_第3頁
C# 數(shù)組5完整版課件_第4頁
C# 數(shù)組5完整版課件_第5頁
已閱讀5頁,還剩48頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第5章數(shù)組本章節(jié)主要包括以下內(nèi)容:數(shù)組結(jié)構(gòu)類型枚舉回顧C(jī)語言中數(shù)組和結(jié)構(gòu)類型特征C數(shù)組結(jié)構(gòu)體數(shù)組、枚舉、結(jié)構(gòu)的區(qū)別不同類型的變量作為一個整體

存貯相同類型常量的值存貯相同類型變量的值數(shù)組枚舉結(jié)構(gòu)張三,計(jì)算機(jī)專業(yè),男,24歲,175cm李四,英語專業(yè),男,25歲,178cm張九,管理專業(yè),男,21歲,170cm學(xué)生姓名,專業(yè),性別,年齡,身高抽象結(jié)構(gòu)intscore1;System.Console.ReadLine(score1)intscore2;System.Console.ReadLine(score2)intscore3;System.Console.ReadLine(score3)intscore4;System.Console.ReadLine(score4)intscore5;System.Console.ReadLine(score5)intscore6;System.Console.ReadLine(score6)intscore7;System.Console.ReadLine(score7)第七位學(xué)生的分?jǐn)?shù)第六位學(xué)生的分?jǐn)?shù)第五位學(xué)生的分?jǐn)?shù)第四位學(xué)生的分?jǐn)?shù)第三位學(xué)生的分?jǐn)?shù)第二位學(xué)生的分?jǐn)?shù)第一位學(xué)生的分?jǐn)?shù)score[6]score[5]score[4]score[3]score[1]score[0]score[2]System.Array應(yīng)用程序數(shù)組存儲學(xué)員的分?jǐn)?shù)intscore[

]=newint[7];6754321在數(shù)組的術(shù)語中,元素表示數(shù)組中存儲的值,數(shù)組長度指數(shù)組中存儲的值的總數(shù),數(shù)組秩指數(shù)組的維數(shù)System.Array包含一些變量的數(shù)據(jù)結(jié)構(gòu),這些變量被稱為元素?cái)?shù)組中所有的元素類型必須相同數(shù)組的索引從0開始數(shù)組是類,具體創(chuàng)建的數(shù)組是對象數(shù)組可以:是一維的,只有一個下標(biāo)是多維的,有多個下標(biāo)數(shù)組有方法System.Array數(shù)組定義:數(shù)據(jù)類型[

]數(shù)組名稱;int[]MyArray={1,2,3,4,5,6,7};MyArray[0],MyArray[1],MyArray[2]…………MyArray[6]

MyArray[0]=1可以執(zhí)行各種操作,如存儲、檢索、排序和反轉(zhuǎn)System.Array如何簡易地執(zhí)行對數(shù)組的操作?創(chuàng)建數(shù)組通過在變量類型后添加一對方括號聲明數(shù)組創(chuàng)建數(shù)組的示例int[]numbers=newint[5]; 創(chuàng)建類型為Object的數(shù)組object[]animals=newobject[100];

int[]MyIntegerArray;C#中的一維數(shù)組回顧:為什么要使用數(shù)組?如何聲明一維數(shù)組?聲明數(shù)組的方法C#int[]arr1;//數(shù)據(jù)類型[]數(shù)組名;intarr1[];//數(shù)據(jù)類型數(shù)組名[];

×√在C#中聲明數(shù)組:數(shù)據(jù)類型[]數(shù)組名不能將數(shù)組名放在數(shù)據(jù)類型和方括號之間!數(shù)組初始化格式靜態(tài)初始化動態(tài)初始化數(shù)據(jù)類型[]數(shù)組名={元素1[,元素2...]};數(shù)組名=new數(shù)據(jù)類型[數(shù)組長度];數(shù)組名=new數(shù)據(jù)類型[數(shù)組長度]{值1,值2…..值n};一維數(shù)組初始化如何設(shè)置大小及初始化?//使用

new設(shè)置大小//創(chuàng)建長度為5的整型數(shù)組int[]array=new

int[5];//創(chuàng)建的同時初始化int[

]arr

=new

int[5]{0,1,2,3,4};

int[

]arr

=new

int[

]{0,1,2,3,4};//省略長度int[

]arr={0,1,2,3,4};//省略new[5]——方括號中的數(shù)字決定數(shù)組的長度{0,1,2,3,4}——大括號中的元素個數(shù)決定數(shù)組的長度 staticvoidMain() {

bool[]a=newbool[2]; char[]b=newchar[3]; double[]c=newdouble[4]; string[]d=newstring[5]; Console.WriteLine(a[0]); Console.WriteLine(b[0]); Console.WriteLine(c[0]); Console.WriteLine(d[0]); }動態(tài)初始化數(shù)組元素的默認(rèn)值小結(jié)string[

]arr3;arr3=newstring[3]{

"I",

"like",

"C#"

};int

arr1[]=newint[3];int[]arr2=newint[3]{1,2};int[]arr1√newint[3]{1,2,0}正誤判斷:××int

[

]arr4

=newstring[3];×類型應(yīng)一致常見錯誤數(shù)組初始值的數(shù)目與數(shù)組的長度不一樣//循環(huán)打印數(shù)組元素int[]array=newint[5]{0,1,2};//聲明并初始化一維數(shù)組for(inti=0;i<array.Length;i++)//輸出數(shù)組中的所有元素{

Console.WriteLine(array[i]);}初始值的個數(shù)必須與數(shù)組的長度一樣!代碼錯誤初始化并訪問數(shù)組成員初始化數(shù)組訪問數(shù)組成員int[]numbers={10,9,8,7,6,5,4,3,2,1,0};numbers[4]=6;

string[]animal={"Mouse","Cat","Lion"};animal[1]=“Cat";stringsomeAnimal=animal[2];數(shù)組元素的訪問在C#中是通過數(shù)組名和下標(biāo)來訪問數(shù)組元素的如何將數(shù)組全部元素相加

int[]a=newint[12];intc=5;intb=6;

a[b+c]+=2;intarr[1];√√int[]a={4,5,6,1,2};for(inti=0;i<=a.Length;i++){sum+=a[i]};Console.WriteLine(sum);System.Array

的屬性和方法屬性Length方法BinarySearchClearCopyRankIsReadOnlyIsFixedSizeCopyToCreateInstanceGetLengthGetLowerBoundGetUpperBoundGetValueIndexOfLastIndexOfReverseSetValueSort一維數(shù)組長度獲得數(shù)組的長度(元素的個數(shù)):數(shù)組名.Length常作為循環(huán)的條件演示:一維數(shù)組//循環(huán)打印數(shù)組元素int[]array=newint[5]{0,1,2,3,4};//聲明并初始化一維數(shù)組for(inti=0;i<array.Length;i++)//輸出數(shù)組中的所有元素{

Console.WriteLine(array[i]);}遍歷整個數(shù)組使用foreach

語句遍歷數(shù)組中的每一個元素int[]numbers={4,5,6,1,2,3,-2,-1,0};foreach(intiinnumbers){

Console.WriteLine(i);}

staticvoidMain(){

int[]nums=newint[]{10,8,36,12,24};

foreach(intjinnums){Console.Write(“{0}\0”,j);}

Console.WriteLine();

for(intj=nums.Length-1;j>=0;j--)

Console.Write(“{0}\0”,nums[j]);

Console.WriteLine();}

運(yùn)行結(jié)果如下:數(shù)組元素倒輸出棧:限定僅在表尾進(jìn)行插入或刪除操作的線性表表尾—棧頂表頭—棧底不含元素的空表稱空棧特點(diǎn)先進(jìn)后出(FILO)后進(jìn)先出(LIFO)ana1a2……...棧底棧頂...出棧進(jìn)棧棧s=(a1,a2,……,an)用一維數(shù)組模擬棧操作staticvoidMain(){stringtest;intmaxlength=50;char[]str=newchar[maxlength];inti;intcurrentpos=0;Console.WriteLine("請輸入要測試的字符串:");test=Console.ReadLine();for(i=0;i<test.Length;i++)//進(jìn)棧{if(currentpos>=maxlength)break;str[currentpos]=test[i];currentpos++;}Console.Write(“輸入字符串的反序是:”);//出棧for(i=0;i<test.Length;i++){if(currentpos<=0)break;Console.Write(str[currentpos]);currentpos--;}用一維數(shù)組模擬棧操作二維數(shù)組二維數(shù)組的定義

數(shù)據(jù)類型[,]數(shù)組名;double[,]dArr;//定義一個double型二維數(shù)組二維數(shù)組的初始化格式:1.數(shù)組名=new數(shù)據(jù)類型[數(shù)組長度1,數(shù)組長度2];

2.將二維數(shù)組的定義與動態(tài)初始化合并在一條語句中

在動態(tài)初始化二維數(shù)組時,也可以直接為其賦予初始化值

dArr=newdouble[3,4]double[,]dArr=newdouble[3,4];int[,]IntArr=newint[,]{{1,3},{2,4},{5,6}};靜態(tài)初始化動態(tài)初始化二維數(shù)組的訪問編程int[,]a=newint[5,5];for(inti=0;i<5;i++){for(intj=0;j<5;j++){if((i==j)||(j==4-i)){a[i,j]=1;}label1.Text+=a[i,j].ToString()+"";}label1.Text+="\n";}練習(xí):求5*5矩陣中的最大值及其所在的位置staticvoidMain(string[]args){int[,]x=newint[9,9];for(inti=0;i<9;i++){for(intj=0;j<9;j++){x[i,j]=(i+1)*(j+1);}}for(inti=0;i<9;i++){for(intj=0;j<9;j++){//x[i,j]=(i+1)*(j+1);Console.Write("{0,5:d}",x[i,j]);}Console.WriteLine();}Console.ReadLine();}

二維數(shù)組的訪問編程:輸出實(shí)現(xiàn)9*9乘法表staticvoidMain(){int[,]a=newint[6,6]; a[0,0]=1;

for(inti=1;i<=5;i++) { a[i,0]=1;

a[i,i]=1;

for(intj=1;j<=i;j++) {

a[i,j]=a[i-1,j-1]+a[i-1,j]; } }

for(inti=0;i<=5;i++) {

for(intj=0;j<=i;j++) { Console.Write("{0}\0",a[i,j]); }

Console.WriteLine(); }}編程:輸出如右圖所示的楊輝三角。矩陣轉(zhuǎn)置問題publicpartialclassForm1:Form{int[,]a=newint[5,5];publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){for(inti=0;i<5;i++){for(intj=0;j<5;j++){a[i,j]=i+2*j;label2.Text+=a[i,j].ToString()+"";

}label2.Text+="\n";}}privatevoidbutton1_Click(objectsender,EventArgse){int[,]b=newint[5,5];for(inti=0;i<5;i++){for(intj=0;j<5;j++){b[i,j]=a[j,i];label4.Text+=b[i,j].ToString()+"";}label4.Text+="\n";}}實(shí)驗(yàn)冒泡排序冒泡排序:每次比較相鄰兩數(shù)小的交換到前面每輪結(jié)束后最大的數(shù)交換到最后16259902316259239092523第一輪:比較了4次第二輪:比較了3次第三輪:比較了2次第四輪:比較了1次用二重循環(huán)實(shí)現(xiàn)冒泡排序i=0i=1i=2i=3第一輪:比較了4次第二輪:比較了3次第三輪:比較了2次第四輪:比較了1次外層循環(huán)如何用二重循環(huán)將5個數(shù)字排序?N=55個數(shù)字存放在一維數(shù)組中外層循環(huán)控制比較多少輪,循環(huán)變量

i內(nèi)層循環(huán)控制每輪比較多少次,循環(huán)變量

j內(nèi)層循環(huán)循環(huán)4次:j<4循環(huán)3次:j<3循環(huán)2次:j<2循環(huán)1次:j<1i<4i<N-1j<?j<4-ij<N-1-i用二重循環(huán)實(shí)現(xiàn)冒泡排序代碼框架:演示示例:將5個學(xué)員成績排序,使用斷點(diǎn)跟蹤for(i=0;i<N-1;i++){

for(j=0;j<N-1-i

;j++){//比較

j和

j+1位置的元素

//如果前大后小就交換

}}冒泡排序速記口訣(升序):N個數(shù)字來排隊(duì)兩兩相比小靠前外層循環(huán)N-1內(nèi)層循環(huán)N-1-i冒泡排序關(guān)鍵代碼分析:for(i=0;i<scores.Length-1;i++){

for(j=0;j<scores.Length-1-i

;j++){

if(scores[j]>scores[j+1]){

//交換元素temp=scores[j];scores[j]=scores[j+1];scores[j+1]=temp;}}}經(jīng)過一輪比較交換,最大的元素?fù)Q到了最后面外層循環(huán)終止條件:數(shù)組長度-1內(nèi)層循環(huán)終止條件:數(shù)組長度–1-i34

結(jié)構(gòu)體是一種構(gòu)造數(shù)據(jù)類型用途:把不同類型的數(shù)據(jù)組合成一個整體-------自定義數(shù)據(jù)類型

struct[結(jié)構(gòu)體類型名]{public類型標(biāo)識符成員名;

public類型標(biāo)識符成員名;

viod

structmethod()

{//方法可執(zhí)行代碼}…………….}結(jié)構(gòu)類型結(jié)構(gòu)體類型定義格式

structRectangle//定義名為Student的結(jié)構(gòu)

{publicdouble長;publicdouble寬;publicvoidArea(){

Console.WriteLine(“面積={0}”,長*寬);}}structstructEx{publicintstructDataMember;

publicvoidstructMethod1(){ //structMethod1實(shí)現(xiàn)}}自定義數(shù)據(jù)類型可以在其內(nèi)部定義方法無法實(shí)現(xiàn)繼承屬于值類型主要結(jié)構(gòu):structstudent{ publicintstud_id; publicstringstud_name; publicfloatstud_marks; publicvoidshow_details() { //顯示學(xué)生詳細(xì)信息 }}結(jié)構(gòu)數(shù)據(jù)成員方法所有與Student關(guān)聯(lián)的詳細(xì)信息都可以作為一個整體進(jìn)行存儲和訪問結(jié)構(gòu)成員的訪問

靜態(tài)成員:成員名前有static關(guān)鍵字

實(shí)例成員:通過創(chuàng)建結(jié)構(gòu)類型的變量來訪問結(jié)構(gòu)的實(shí)例成員訪問格式:結(jié)構(gòu)名.靜態(tài)成員名訪問格式:結(jié)構(gòu)名標(biāo)識符;12結(jié)構(gòu)體變量和結(jié)構(gòu)體數(shù)組的使用結(jié)構(gòu)體變量的使用

結(jié)構(gòu)體數(shù)組的使用變量定義格式:結(jié)構(gòu)體類型名變量名1,變量名2,…..;studentstu1,stu2;結(jié)構(gòu)體變量不能整體引用,只能引用變量成員,其規(guī)則是:結(jié)構(gòu)體變量名.成員名1stu1.num=10;=“王華”;2student[]stu=newstudent[5];Stu[1].name=“李俊“;統(tǒng)計(jì)候選人選票例namecountLiZhangWang000usingSystem;namespaceSample01{

structperson{publicstringname;publicintcount;}classProgram{staticvoidMain(string[]args){person[]leader=newperson[3];leader[0].name="Li";leader[1].name="Zhang";leader[2].name="Wang";統(tǒng)計(jì)候選人選票

練習(xí):建立10名學(xué)生的信息表,每個學(xué)生的數(shù)據(jù)包括學(xué)號、姓名和三門課的成績,求出每個學(xué)生的總分和平均分,在屏幕上把學(xué)生信息、總分和平均分輸出出來。inti,j;stringleader_name;for(i=1;i<=4;i++){

leader_name=Console.ReadLine();for(j=0;j<3;j++)if(leader_name.Equals(leader[j].name))

leader[j].count++;}for(i=0;i<3;i++)Console.WriteLine("{0,8}:{1}",leader[i].name,

leader[i].count);}}}

順序查找算法見動畫演示label1label2利用結(jié)構(gòu)類型,聲明一個結(jié)構(gòu)數(shù)組,輸入不超過10個學(xué)生的信息,顯示全部信息和查詢某學(xué)校的學(xué)生情況。程序代碼publicstructstudent{publicstringname,sex;publicstaticinti=0;}publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}

student[]stu=newstudent[10];privatevoidbutton1_Click(objectsender,EventArgse){

stu[student.i].name=textBox1.Text;stu[student.i].sex=textBox2.Text;label1.Text=label1.Text+"\n"+stu[student.i].name+""+stu[student.i].sex;textBox1.Text="";textBox2.Text="";}privatevoidbutton2_Click(objectsender,EventArgse){StringTSchool=textBox3.Text;

for(intk=0;k<=student.i;k++){if(stu[k].name==TSchool){label2.Text+=stu[k].name+""+stu[k].sex;}}}privatevoidForm1_Load(objectsender,EventArgse){label2.Text="姓名性別"+"\n";label1.Text="姓名性別";}枚舉枚舉(Enum)是一組已命名的數(shù)值常量

用于定義具有一組特定值的數(shù)據(jù)類型枚舉以enum

關(guān)鍵字聲明格式:

enum

枚舉名

{枚舉成員表}[;]枚舉C#中的枚舉包含與值關(guān)聯(lián)的數(shù)字默認(rèn)情況下,將0值賦給枚舉的第一個元素,然后對每個后續(xù)的枚舉元素按1遞增在初始化過程中可重寫默認(rèn)值publicenum

Color{

Red=1,

Blue=2,

Green=3,

Black=4,

Yellow=5}枚舉publicclassColorTest{publicenumColor{

Red,

Blue,

Green,

Black,

Yellow}publicvoidSetColor(Colorc){//處理顏色}staticvoidMain(){

ColorTest

c=newColorTest();

c.SetColor(ColorTest.Color.Red);}}Red=0

枚舉成員的賦值usingSystem;enumcolor{yellow,brown,blue,black,purple}classEnumDemo{staticvoidMain(){

Console.WriteLine(“yellow={0}”,color.yellow);

Console.WriteLine(“yellow={0}”,(int)color.yellow);

Console.WriteLine(“brown={0}”,(int)color.brown);

Console.WriteLine(“blue={0}”,(int)color.blue);

Console.WriteLine(“black={0}”,(int)color.black);

Console.WriteLine(“purple={0}”,(int)color.purple);}}注意:枚舉成員的值在不經(jīng)過顯式轉(zhuǎn)換前,是不會變換成整數(shù)值的。默認(rèn)情況下C#規(guī)定第1個枚舉成員的值取0,它后面的每一個枚舉成員的值按加上1遞增。usingSystem;enumcolor{yellow=-1,brown,blue,black,purple}classEnumDemo{staticvoidMain(){

Console.WriteLine(“yellow={0}”,color.yellow);

Console.WriteLine(“yellow={0}”,(int)color.yellow);

Console.WriteLine(“brown={0}”,(int)color.brown);

Console.WriteLine(“blue={0}”,(int)color.blue);

Console.WriteLine(“black={0}”,(int)color.black);

Console.WriteLine(“purple={0}”,(int)color.purple);}}為第1個枚舉成員賦值,它后面的每一個枚舉成員的值按加1遞增。usingSystem;enumcolor{yellow,brown,blue,black=6,purple}classEnumDemo{staticvoidMain(){

Console.WriteLine(“yellow={0}”,color.yellow);

Console.WriteLine(“yellow={0}”,(int)color.yellow);

Console.WriteLine(“brown={0}”,(int)color.brown);

Console.WriteLine(“blue={0}”,(int)color.blue);

Console.WriteLine(“black={0}”,(int)color.black);

Console.WriteLine(“purple={0}”,(int)color.purple);}}直接為某個枚舉成員賦值,則其他枚舉成員依次取值,例:輸出枚舉成員對應(yīng)的整數(shù)值。usingSystem;enumcolor{yellow,brown=3,blue,black=-3,purple}classEnumDemo{staticvoidMain(){

Console.WriteLine(“yellow={0}”,color.yellow);

Console.WriteLine(“yellow={0}”,(int)color.yellow);

Console.WriteLine(“brown={0}”,(int)color.brown);

Console.WriteLine(“blue={0}”,(int)color.blue);

Console.WriteLine(“black={0}”,(int)color.black);

Console.WriteLine(“purple={0}”,(int)color.purple);}}為多個枚舉成員賦值例:輸出枚舉成員對應(yīng)的整數(shù)值。usingSystem;classEnumDemo{

enumcolor{yellow,brown=3,blue,black=blue,purple}staticvoidMain(){

Console.WriteLine(“yellow={0}”,color.yellow);

Console.WriteLine(“yellow={0}”,(int)color.yellow);

Console.WriteLine(“brown={0}”,(int)color.brown);

Console.WriteLine(“blue={0}”,(int)color.blue);

Console.WriteLine(“black={0}”,(int)color.black);

Console.WriteLine(“purple={0}”,(int)color.purple);}}多個枚舉成員具有同樣的整數(shù)值枚舉成員的訪問

enum

WeekDay{Sun,Mon,Tue,Wed,Thu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論