欢迎来到天天文库
浏览记录
ID:34071899
大小:54.80 KB
页数:3页
时间:2019-03-03
《arrays - quia阵列-全能》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、ArraysArraysaredatastructuresconsistingofrelateddataitemsofthesametypeusingcontiguousmemorylocations.We’llbeusingstaticarrays(staysthesameuseoncecreated).ArrayelementsarenumberedthesameasC++numberingscheme.(0èn-1,wherenisthearraysize.)myarray[0]=7myarray[1]=198myarray[2]
2、=-225myarray[3]=-75myarray[4]=4myarray[5]=118myarray[6]=-36myarray[7]=94EveryarrayisJavaknowsitsownlengthandmaintainsthisinformationinavariablecalledlength.myarray.lengthArrays:DeclaringandAllocatingArraysareallocateddynamicallywiththenewoperator.intmyarray[]=newint[4];o
3、rintmyarray[];myarray=newint[4];orStringpalindrome[]=newString[50],reverse[]=newString[50];ordouble[]sal1,sal2;//arraytypeandbracketscanbeusedatthe//beginningtoindicatethatallidentifierslistedrepresentarrays.Allocatingarraysandinitializingitselements:Lookp318InitArrayPro
4、gramInitializerListsinttemp[]={50,60,70,80,90,100};arraysizeisdeterminedbythenumberofelementsintheinitializerlist.newisnotrequiredwhenusinginitializerlists.Lookp320UseofinitializerlistfinalislikeconstfromC++ExamplesofArrays:SummingtheElementsofanArrayp322Histogramsp323**
5、*ElementsasCountersp325(RollDie)StudentPollProgramp326Assignment:FrequencyofDieSumsProgramSimulatetherollingoftwodice.Useyourroll()method.Sumtherolls(numbersshouldvaryfrom2to12).Yourprogramshouldrollthedie36,000times.Useasingle-subscriptedarraytotallythenumbersoftimeseac
6、hpossiblesumappears.DisplaythecompletefrequencyarrayinadialogboxusingJTextArea.Lookatpage325.
此文档下载收益归作者所有