資源描述:
《arrays - quia陣列-全能》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
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.