資源描述:
《java 數(shù)組的應(yīng)用ppt課件.ppt》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、ArraysArraysareobjectsthathelpusorganizelargeamountsofinformationChapter8focuseson:arraydeclarationanduseboundscheckingandcapacityarraysthatstoreobjectreferencesvariablelengthparameterlistsmultidimensionalarraystheArrayListclass2OutlineDeclaringandUsingArraysArraysofObj
2、ectsVariableLengthParameterListsTwo-DimensionalArraysTheArrayListClass3ArraysAnarrayisanorderedlistofvalues012345678979879482679887817491AnarrayofsizeNisindexedfromzerotoN-1scoresTheentirearrayhasasinglenameEachvaluehasanumericindexThisarrayholds10valuesthatareindexedfr
3、om0to94ArraysAparticularvalueinanarrayisreferencedusingthearraynamefollowedbytheindexinbracketsForexample,theexpressionscores[2]referstothevalue94(the3rdvalueinthearray)Thatexpressionrepresentsaplacetostoreasingleintegerandcanbeusedwhereveranintegervariablecanbeused5Arr
4、aysForexample,anarrayelementcanbeassignedavalue,printed,orusedinacalculation:scores[2]=89;scores[first]=scores[first]+2;mean=(scores[0]+scores[1])/2;System.out.println("Top="+scores[5]);6ArraysThevaluesheldinanarrayarecalledarrayelementsAnarraystoresmultiplevaluesofthes
5、ametype–theelementtypeTheelementtypecanbeaprimitivetypeoranobjectreferenceTherefore,wecancreateanarrayofintegers,anarrayofcharacters,anarrayofStringobjects,anarrayofCoinobjects,etc.InJava,thearrayitselfisanobjectthatmustbeinstantiated7ArraysAnotherwaytodepictthescoresar
6、ray:scores798794826798878174918DeclaringArraysThescoresarraycouldbedeclaredasfollows:int[]scores=newint[10];Thetypeofthevariablescoresisint[](anarrayofintegers)Notethatthearraytypedoesnotspecifyitssize,buteachobjectofthattypehasaspecificsizeThereferencevariablescoresiss
7、ettoanewarrayobjectthatcanhold10integers9DeclaringArraysSomeotherexamplesofarraydeclarations:float[]prices=newfloat[500];boolean[]flags;flags=newboolean[20];char[]codes=newchar[1750];10UsingArraysTheiteratorversionoftheforloopcanbeusedwhenprocessingarrayelementsfor(ints
8、core:scores)System.out.println(score);Thisisonlyappropriatewhenprocessingallarrayelementsfromtop(lowestindex)t