资源描述:
《Java语言基础语法bppt课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Java程序设计JavaProgrammingFall,2007成都信息工程学院计算机系ContentsArrays(数组)String(字符串类)StringBufferArraysAnarrayisacollectionofdatavalues,allofwhichhavethesametype.Components:PrimitivetypesorreferencetoobjectsArraysareobjectsinJava.ArraysAnarrayvariableisdeclaredwiththetyp
2、eofthemembers.Forinstance,thefollowingisadeclarationofavariableofanarraywithmembersofthetypedouble:double[]anArray;Whenanarrayvariableisdeclared,thearrayisNOTcreated.Whatwehaveisonlyanameofanarray.anArraywillhavethespecialvaluenulluntilitiscreated.CreatingArra
3、ysTocreatethearray,operatornewisused.Wemustprovidethenumberofmembersinthearray,andthetypeofthemembers.Thesecannotbechangedlater.double[]anArray;anArray=newdouble[5];or,combiningthedeclarationandthearraycreation:double[]anArray=newdouble[5];Whenanarrayiscreated
4、,thenumberofpositionsavailableinthearraycanbeaccessedusingafieldcalledlengthwiththedotoperator.Forinstance,anArray.lengthhasavalue5.Arraycreationdouble[]anArray;anArray=newdouble[3]anArray:???length3012anArray:nullHere,thearrayisnotdefined.Here,thearrayisdefin
5、ed,buttheelementsinthearrayareNOTdefined.ArraysThesizeofthearrayisfixed(固定的)atcreation.Torefertospecificvalueswithinthearray,anarrayindexisusedtorefertoaspecificpositioninthearray.InJava,foranarrayoflengthn,thevaluesinthearrayareindexedfrom0upton–1.Notation:ar
6、eferstotheentirecollectiona[i]referstothevalueatindexiAccessingarraymembersArraymembersareaccessedbyindicesusingthesubscriptoperator[].Theindicesareintegersstartingfrom0.Forinstance,ifanArrayisanarrayofthreeintegers,then:thefirstmemberisanArray[0]thesecondmemb
7、erisanArray[1],andthethirdmemberisanArray[2].Theindicescanbeanyexpressionthathasanintegervalue.Ifanindexisoutofrange,i.e.,lessthan0orgreaterthanlength-1,arun-timeerroroccurs.InitializingarraymembersArraymemberscanbeinitializedindividuallyusingtheindicesandthes
8、ubscriptoperator.int[]intArray=newint[3];intArray[0]=3;intArray[1]=5;intArray[2]=4;Arraymembersmayalsobeinitializedwhenthearrayiscreated:int[]intArray;intArray=newint[]{3,5,4};Arra