java語言程序設(shè)計(jì)基礎(chǔ)篇課件(第7章)英文_第1頁
java語言程序設(shè)計(jì)基礎(chǔ)篇課件(第7章)英文_第2頁
java語言程序設(shè)計(jì)基礎(chǔ)篇課件(第7章)英文_第3頁
java語言程序設(shè)計(jì)基礎(chǔ)篇課件(第7章)英文_第4頁
java語言程序設(shè)計(jì)基礎(chǔ)篇課件(第7章)英文_第5頁
已閱讀5頁,還剩23頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

Chapter7MultidimensionalArrays1MotivationsThusfar,youhaveusedone-dimensionalarraystomodellinearcollectionsofelements.Youcanuseatwo-dimensionalarraytorepresentamatrixoratable.Forexample,thefollowingtablethatdescribesthedistancesbetweenthecitiescanberepresentedusingatwo-dimensionalarray.2ObjectivesTogiveexamplesofrepresentingdatausingtwo-dimensionalarrays(§7.1).Todeclarevariablesfortwo-dimensionalarrays,createarrays,andaccessarrayelementsinatwo-dimensionalarrayusingrowandcolumnindexes(§7.2).Toprogramcommonoperationsfortwo-dimensionalarrays(displayingarrays,summingallelements,findingminandmaxelements,andrandomshuffling)(§7.3).Topasstwo-dimensionalarraystomethods(§7.4).Towriteaprogramforgradingmultiple-choicequestionsusingtwo-dimensionalarrays(§7.5).Tosolvetheclosest-pairproblemusingtwo-dimensionalarrays(§7.6).TocheckaSudokusolutionusingtwo-dimensionalarrays(§7.7).Tousemultidimensionalarrays(§7.8).3Declare/CreateTwo-dimensionalArrays//DeclarearrayrefvardataType[][]refVar;//CreatearrayandassignitsreferencetovariablerefVar=newdataType[10][10];//CombinedeclarationandcreationinonestatementdataType[][]refVar=newdataType[10][10];//AlternativesyntaxdataTyperefVar[][]=newdataType[10][10];4DeclaringVariablesofTwo-dimensionalArraysandCreatingTwo-dimensionalArraysint[][]matrix=newint[10][10];orintmatrix[][]=newint[10][10];matrix[0][0]=3;for(inti=0;i<matrix.length;i++)for(intj=0;j<matrix[i].length;j++)matrix[i][j]=(int)(Math.random()*1000);double[][]x;5Two-dimensionalArrayIllustration6array.length?4array[0].length?3matrix.length?5matrix[0].length?5Declaring,Creating,andInitializingUsingShorthandNotationsYoucanalsouseanarrayinitializertodeclare,createandinitializeatwo-dimensionalarray.Forexample,7int[][]array=newint[4][3];array[0][0]=1;array[0][1]=2;array[0][2]=3;array[1][0]=4;array[1][1]=5;array[1][2]=6;array[2][0]=7;array[2][1]=8;array[2][2]=9;array[3][0]=10;array[3][1]=11;array[3][2]=12;

int[][]array={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};SameasLengthsofTwo-dimensionalArraysint[][]x=newint[3][4];8LengthsofTwo-dimensionalArrays,[][]array={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};9array.lengtharray[0].lengtharray[1].lengtharray[2].lengtharray[3].lengtharray[4].lengthArrayIndexOutOfBoundsExceptionRaggedArraysEachrowinatwo-dimensionalarrayisitselfanarray.So,therowscanhavedifferentlengths.Suchanarrayisknownasaraggedarray.Forexample,int[][]matrix={{1,2,3,4,5},{2,3,4,5},{3,4,5},{4,5},{5}};10matrix.lengthis5matrix[0].lengthis5matrix[1].lengthis4matrix[2].lengthis3matrix[3].lengthis2matrix[4].lengthis1RaggedArrays,cont.11ProcessingTwo-DimensionalArraysSeetheexamplesinthetext.(Initializingarrayswithinputvalues)(Printingarrays)(Summingallelements)(Summingallelementsbycolumn)(Whichrowhasthelargestsum)(Findingthesmallestindexofthelargestelement)(Randomshuffling)12Initializingarrayswithinputvaluesjava.util.Scannerinput=newScanner(System.in);System.out.println("Enter"+matrix.length+"rowsand"+matrix[0].length+"columns:");for(introw=0;row<matrix.length;row++){

for(intcolumn=0;column<matrix[row].length;column++){matrix[row][column]=input.nextInt();}}13Initializingarrayswithrandomvaluesfor(introw=0;row<matrix.length;row++){

for(intcolumn=0;column<matrix[row].length;column++){matrix[row][column]=(int)(Math.random()*100);}}14Printingarraysfor(introw=0;row<matrix.length;row++){

for(intcolumn=0;column<matrix[row].length;column++){System.out.print(matrix[row][column]+"");}System.out.println();}15Summingallelementsinttotal=0;for(introw=0;row<matrix.length;row++){

for(intcolumn=0;column<matrix[row].length;column++){total+=matrix[row][column];}}16Summingelementsbycolumnfor(intcolumn=0;column<matrix[0].length;column++){inttotal=0;for(introw=0;row<matrix.length;row++)total+=matrix[row][column];System.out.println("Sumforcolumn"+column+"is"+total);}17Randomshufflingfor(inti=0;i<matrix.length;i++){

for(intj=0;j<matrix[i].length;j++){

inti1=(int)(Math.random()*matrix.length);

intj1=(int)(Math.random()*matrix[i].length);//Swapmatrix[i][j]withmatrix[i1][j1]inttemp=matrix[i][j];matrix[i][j]=matrix[i1][j1];matrix[i1][j1]=temp;}}18PassingTow-DimensionalArraystoMethods19PassTwoDimensionalArrayRunProblem:GradingMultiple-ChoiceTestObjective:writeaprogramthatgradesmultiple-choicetest.20GradeExamRunProblem:FindingTwoPointsNearesttoEachOther21FindNearestPointsRunWhatisSudoku?22Everyrowcontainsthenumbers1to923Everycolumncontainsthenumbers1to924

534

678

9

1

2

67

2195

3

4

8

1983

4

2

567

85

9

76

1

4

23

42

68537

9

1

71

3

924

8

56

961

5

3

7

2

8

4

2

8

7419

6

35

3

4

5

2

86

179

Every3×3boxcontainsthenumbers1to925

534

678

9

1

2

67

2195

3

4

8

1983

4

2

567

85

9

76

1

4

23

42

68537

9

1

71

3

924

8

56

961

5

3

7

2

8

4

2

8

7419

6

35

3

4

5

2

86

179

CheckingWhetheraSolutionIsCorrect26RunCheckSudokuSolutionMultidimensionalArraysOccasionally,youwillneedtorepresentn-dimensionaldatastructures.InJava,youcancreaten-dimensionalarraysforanyintegern.

Thewaytodeclaretwo-dimensionalarrayvariablesandcreatetwo-dimensionalarrayscanbegeneralizedtodeclaren-dimensionalarrayvariablesandcreaten-dimensionalarraysforn>=3.Forexample,thefollowingsyntaxdeclares

溫馨提示

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

最新文檔

評論

0/150

提交評論