欢迎来到天天文库
浏览记录
ID:59417356
大小:209.50 KB
页数:47页
时间:2020-09-19
《java 数组的应用ppt课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
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
此文档下载收益归作者所有