Java课件第二章补充数组.ppt
《Java课件第二章补充数组.ppt》由会员分享,可在线阅读,更多相关《Java课件第二章补充数组.ppt(37页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 2 ArrayChapter 2 Array2.1DeclaringandCreatingArrays2.1DeclaringandCreatingArrays2.4CopyingArrays2.4CopyingArrays2.2ArrayInitialize2.2ArrayInitialize2.3Two-demensionArrays2.3Two-demensionArrays2.5PassingArraytomethods2.5PassingArraytomethodsIntroducing ArraysIntroducing ArraysArrayisadatastru
2、cturethatrepresentsacollectionofArrayisadatastructurethatrepresentsacollectionofthesametypesofdata.thesametypesofdata.Declaring Array VariablesDeclaring Array VariablesdatatypedatatypearrayRefVararrayRefVar;Example:Example:doubledoublemyListmyList;datatypedatatype arrayRefVararrayRefVar;/Thisstyleis
3、correct,/Thisstyleiscorrect,butnotpreferredbutnotpreferredExample:Example:doubledoublemyListmyList;v intintaa;=intintaa;v intintaa,b b;intintaa,b b;Creating ArraysCreating ArraysFormat:Format:arrayRefVararrayRefVar=new=newdatatypearraySizedatatypearraySize;Example:Example:myListmyList=newdouble10;=n
4、ewdouble10;DefaultValue:DefaultValue:Whenanarrayiscreated,itselementsareWhenanarrayiscreated,itselementsareassignedthedefaultvalueofassignedthedefaultvalueof 0forthenumericprimitivedatatypes0forthenumericprimitivedatatypesu0000forchartypesu0000forchartypesfalseforbooleantypesfalseforbooleantypesCrea
5、ting ArraysCreating Arrayse.g.:myList.lengthreturns10e.g.:myList.lengthreturns10TheLengthofanArray:TheLengthofanArray:Onceanarrayiscreated,itssizeisfixed.ItOnceanarrayiscreated,itssizeisfixed.Itcannotbechanged.Youcanfinditssizeusingcannotbechanged.YoucanfinditssizeusingarrayRefVar.lengtharrayRefVar.
6、lengthuseusenewnewtocreat:tocreat:c=newchar5c=newchar5;u000u0000 0 u000u0000 0 u000u0000 0 u000u0000 0 u000u0000 05 50 x12340 x1234vv(c ch ha ar r a ar rr ra ay y,5 5 c ch ha ar r d da at ta a )cCreating ArraysCreating ArraysCreatingArrayCreatingArray:I.referencedatatypereferencedatatypeArray:Array:
7、II.Pointp=newPointp=newPoint3Point3;nullnull nullnullnullnull3 30 x12450 x1245vv(P Po oi in nt t A Ar rr ra ay y,3 3 P Po oi in nt t d da at ta a)p pp0p0p1p1p2p2Creating ArraysCreating ArraysII.weshouldusenewtoallocatememoryweshouldusenewtoallocatememoryforeveryelements.foreveryelements.0,00,0p0p00
8、x12450 x1245p p0,00,0nullnull3 30 x24640 x24640 x24660 x2466p1p1p0=newPointp0=newPoint();();();();p1=newPointp1=newPoint();();();();CreatingArrayCreatingArray:Creating ArraysCreating ArraysDeclaring and Creating in One StepDeclaring and Creating in One StepdatatypedatatypearrayRefVararrayRefVar=new=
9、newdatatypearraySizedatatypearraySize;e.g.:doublee.g.:doublemyListmyList=newdouble10;=newdouble10;datatypedatatype arrayRefVararrayRefVar=new=newdatatypearraySizedatatypearraySize;e.g.:doublee.g.:doublemyListmyList=newdouble10;=newdouble10;0 00 00 03 30 x45670 x4567a ainta=newint3;a0a0a1a1a2a2a.leng
10、tha.lengthExampleExample:Declaring and Creating in One StepDeclaring and Creating in One StepArray InitializeArray InitializeDeclaring,creating,initializinginonestep:e.g.:inta=1e.g.:inta=1,2 2,3 3,4 4,55;Strings=“abc”Strings=“abc”,“def”“def”;Strings=newString(“abc”),Strings=newString(“abc”),newStrin
11、g(“def”);newString(“def”);Thisshorthandsyntaxmustbeinonestatement.doublemyList=1.9,2.9,3.4,3.5;doublemyList=1.9,2.9,3.4,3.5;Thisshorthandnotationisequivalenttothefollowingstatements:doublemyList=newdouble4;doublemyList=newdouble4;myList0=1.9;myList0=1.9;myList1=2.9;myList1=2.9;myList2=3.4;myList2=3.
12、4;myList3=3.5;myList3=3.5;Array InitializeArray InitializeCAUTIONCAUTIONUsingtheshorthandnotation,youhavetodeclare,create,andinitializethearrayallinonestatement.Splittingitwouldcauseasyntaxerror.Forexample,thefollowingiswrong:doublemyList;myList=1.9,2.9,3.4,3.5;ArrayInitialize:Case:Case:ArrayClassOb
13、j.javaArrayClassObj.javaCase:Case:ArrayInit.javaArrayInit.javaArrays Initialize ExampleArrays Initialize Exampleimportjava.util.*;importjava.util.*;publicclassArrayClassObjpublicclassArrayClassObjstaticRandomrand=newRandom();staticRandomrand=newRandom();staticintpRand(intmod)staticintpRand(intmod)re
14、turnMath.abs(rand.nextInt()%mod;returnMath.abs(rand.nextInt()%mod;publicstaticvoidmain(Stringargs)publicstaticvoidmain(Stringargs)Integera=newIntegerpRand(20);Integera=newIntegerpRand(20);prt(lengthofa=+a.length);prt(lengthofa=+a.length);for(inti=0;ia.length;i+)for(inti=0;ia.length;i+)ai=newInteger(
15、pRand(500);ai=newInteger(pRand(500);prt(a+i+=+ai);prt(a+i+=+ai);staticvoidprt(Strings)staticvoidprt(Strings)System.out.println(s);System.out.println(s);ArrayClassObj.javaArrayClassObj.javapublicclassArrayInitpublicclassArrayInitpublicstaticvoidmain(Stringargs)publicstaticvoidmain(Stringargs)Integera
16、=Integera=newInteger(1),newInteger(1),newInteger(2),newInteger(2),newInteger(3),newInteger(3),;/Java1.1only:Integerb=newIntegerIntegerb=newIntegernewInteger(1),newInteger(1),newInteger(2),newInteger(2),newInteger(3),newInteger(3),;Two-dimensional ArraysTwo-dimensional ArraysDeclarearrayDeclarearrayr
17、efvarrefvar:dataTypedataTyperefVarrefVar;e.g.:e.g.:CreatearrayandassignitsreferencetovariableCreatearrayandassignitsreferencetovariablerefVarrefVar=newdataType1010;=newdataType1010;CombinedeclarationandcreationinonestatementCombinedeclarationandcreationinonestatementdataTypedataTyperefVarrefVar=newd
18、ataType1010;=newdataType1010;AlternativesyntaxAlternativesyntaxdataTypedataType refVarrefVar=newdataType1010;=newdataType1010;vv intintaa;vv intintaa;vv intintaa;Two-dimensional Array IllustrationTwo-dimensional Array IllustrationTwo-dimensional ArraysTwo-dimensional ArraysLengthofTwo-dimensionalArr
19、ays:LengthofTwo-dimensionalArrays:e.g.:e.g.:intintx=newint34;x=newint34;Caution:Accessinganarrayoutofboundsisacommonprogrammingerror,whichthrowsaruntimeArrayIndexOutOfBoundsException.Toavoidit,makesurethatyoudonotuseanindexbeyondarrayRefVar.length-1.Two-dimensional ArraysTwo-dimensional Arraysintarr
20、ay=intarray=1,2,3,1,2,3,4,5,6,4,5,6,7,8,9,7,8,9,10,11,1210,11,12;array.lengtharray.lengtharray0.lengtharray0.lengtharray1.lengtharray1.lengtharray2.lengtharray2.lengtharray3.lengtharray3.lengtharray4.lengthArrayIndexOutOfBoundsExceptionarray4.lengthArrayIndexOutOfBoundsExceptionTwo-dimensional Array
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 课件 第二 补充 数组
限制150内