Java课件第二章补充数组_第1页
Java课件第二章补充数组_第2页
Java课件第二章补充数组_第3页
Java课件第二章补充数组_第4页
Java课件第二章补充数组_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

2.1DeclaringandCreatingArrays,2.4CopyingArrays,2.2ArrayInitialize,2.3Two-demensionArrays,2.5PassingArraytomethods,IntroducingArrays,Arrayisadatastructurethatrepresentsacollectionofthesametypesofdata.,DeclaringArrayVariables,datatypearrayRefVar;Example:doublemyList;datatypearrayRefVar;/Thisstyleiscorrect,butnotpreferredExample:doublemyList;,inta;=inta;inta,b;inta,b;,CreatingArrays,Format:arrayRefVar=newdatatypearraySize;Example:myList=newdouble10;,DefaultValue:Whenanarrayiscreated,itselementsareassignedthedefaultvalueof,0forthenumericprimitivedatatypesu0000forchartypesfalseforbooleantypes,CreatingArrays,e.g.:myList.lengthreturns10,TheLengthofanArray:Onceanarrayiscreated,itssizeisfixed.Itcannotbechanged.Youcanfinditssizeusing,arrayRefVar.length,usenewtocreat:c=newchar5;,0 x1234,(chararray,5chardata),c,CreatingArrays,CreatingArray:,referencedatatypeArray:Pointp=newPoint3;,0 x1245,(PointArray,3Pointdata),p,p0,p1,p2,CreatingArrays,weshouldusenewtoallocatememoryforeveryelements.,0,0,0,0,p0=newPoint();p1=newPoint();,CreatingArray:,CreatingArrays,DeclaringandCreatinginOneStep,datatypearrayRefVar=newdatatypearraySize;e.g.:doublemyList=newdouble10;datatypearrayRefVar=newdatatypearraySize;e.g.:doublemyList=newdouble10;,0 x4567,a,inta=newint3;,a0,a1,a2,a.length,Example:,DeclaringandCreatinginOneStep,ArrayInitialize,Declaring,creating,initializinginonestep:,Thisshorthandsyntaxmustbeinonestatement.,doublemyList=1.9,2.9,3.4,3.5;Thisshorthandnotationisequivalenttothefollowingstatements:doublemyList=newdouble4;myList0=1.9;myList1=2.9;myList2=3.4;myList3=3.5;,ArrayInitialize,CAUTION,Usingtheshorthandnotation,youhavetodeclare,create,andinitializethearrayallinonestatement.Splittingitwouldcauseasyntaxerror.Forexample,thefollowingiswrong:doublemyList;myList=1.9,2.9,3.4,3.5;,ArrayInitialize:,Case:ArrayClassObj.javaCase:ArrayInit.java,ArraysInitializeExample,importjava.util.*;publicclassArrayClassObjstaticRandomrand=newRandom();staticintpRand(intmod)returnMath.abs(rand.nextInt()%mod;publicstaticvoidmain(Stringargs)Integera=newIntegerpRand(20);prt(lengthofa=+a.length);for(inti=0;ia.length;i+)ai=newInteger(pRand(500);prt(a+i+=+ai);staticvoidprt(Strings)System.out.println(s);,ArrayClassObj.java,publicclassArrayInitpublicstaticvoidmain(Stringargs)Integera=newInteger(1),newInteger(2),newInteger(3),;/Java1.1only:Integerb=newIntegernewInteger(1),newInteger(2),newInteger(3),;,Two-dimensionalArrays,Declarearrayrefvar:dataTyperefVar;e.g.:CreatearrayandassignitsreferencetovariablerefVar=newdataType1010;CombinedeclarationandcreationinonestatementdataTyperefVar=newdataType1010;AlternativesyntaxdataTyperefVar=newdataType1010;,inta;inta;inta;,Two-dimensionalArrayIllustration,Two-dimensionalArrays,LengthofTwo-dimensionalArrays:e.g.:intx=newint34;,Caution:Accessinganarrayoutofboundsisacommonprogrammingerror,whichthrowsaruntimeArrayIndexOutOfBoundsException.Toavoidit,makesurethatyoudonotuseanindexbeyondarrayRefVar.length-1.,Two-dimensionalArrays,intarray=1,2,3,4,5,6,7,8,9,10,11,12;,array.lengtharray0.lengtharray1.lengtharray2.lengtharray3.length,array4.lengthArrayIndexOutOfBoundsException,Two-dimensionalArrays,Example:,Creating:,【e.g.】inta=newint3;,0 x4978,a,a0,a1,a2,a.length,Two-dimensionalArrays,0 x4978,a,a0,a1,a2,a.length,a=newint3;a0=newint3;,0 x4978,a,a0,a1,a2,a.length,a=newint3;a0=newint3;a1=newint2;,0 x4978,a,a0,a1,a2,a.length,a=newint3;a0=newint3;a1=newint2;a2=newint4;,Declaring,Creating,andInitializingUsingShorthandNotations,Youcanalsouseanarrayinitializertodeclare,createandinitializeatwo-dimensionalarray.Forexample,intarray=newint43;array00=1;array01=2;array02=3;array10=4;array11=5;array12=6;array20=7;array21=8;array22=9;array30=10;array31=11;array32=12;,intarray=1,2,3,4,5,6,7,8,9,10,11,12;,EnhancedforLoop,JDK1.5introducedanewforloopthatenablesyoutotraversethecompletearraysequentiallywithoutusinganindexvariable.Ingeneral,thesyntaxisYoustillhavetouseanindexvariableifyouwishtotraversethearrayinadifferentorderorchangetheelementsinthearray.,for(elementTypevalue:arrayRefVar)/Processthevalue,Arraytest.java,Strings=newString10;a)Thislineofcodeisillegal.b)sisatwo-dimensionalarraycontaining10rowsand10columnsc)Eachelementinsissettod)Eachelementinsisuninitializedandmustbeinitializedbeforeitisreferenced.,CopyingArrays,Often,inaprogram,youneedtoduplicateanarrayorapartofanarray.Insuchcasesyoucouldattempttousetheassignmentstatement(=),asfollows:list2=list1;,CopyingArrays,Usingaloop:intsourceArray=2,3,1,5,10;inttargetArray=newintsourceArray.length;for(inti=0;isourceArrays.length;i+)targetArrayi=sourceArrayi;,ThearraycopyUtility,arraycopy(sourceArray,src_pos,targetArray,tar_pos,length);Example:System.arraycopy(sourceArray,0,targetArray,0,sourceArray.length);,CopyingArray:System.arraycopy(object,int,object,int,int)inta=1,2,3;intb=4,5,6,7,8,9;System.arraycopy(a,0,b,0,a.length);b=1,2,3,7,8,9;,Arraycopy.java,ThearraycopyUtility,PassingArraystoMethods,publicstaticvoidprintArray(intarray)for(inti=0;iarray.length;i+)System.out.print(arrayi+);,Invokethemethodintlist=3,1,2,6,4,2;printArray(list);,InvokethemethodprintArray(newint3,1,2,6,4,2);,AnonymousArray,ThestatementprintArray(newint3,1,2,6,4,2);createsanarrayusingthefollowingsyntax:newdataTypeliteral0,.,literalk;Thereisnoexplicitreferencevariableforthearray.Sucharrayiscalledananonymousarray.,Case:VarArgs.java,classAinti;publicclassVarArgsstaticvoidf(Objectx)for(inti=0;ix.length;i+)System.out.println(xi);publicstaticvoidmain(Stringargs)f(newObjectnewInteger(47),newVarArgs(),newFloat(3.14),newDouble(11.11);f(newObjectone,two,three);f(newObjectnewA(),newA(),newA();,VarArgs.java,PassByValue,Javausespassbyvaluetopassparameterstoamethod.Thereareimportantdifferencesbetweenpassingavalueofvariablesofprimitivedatatypesandpassingarrays.Foraparameterofaprimitivetypevalue,theactualvalueispassed.Changingthevalueoftheloca

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论