Java Reference
In-Depth Information
Chapter 6
Introducing Arrays
A New Data Type
In Chapter4 youlearnedaboutJava'sprimitivedatatypesandaboutthe
Stringclass.TheJavaprimitivedatatypesservetostoreasinglealphanu-
mericorBooleanvalue.Forexample,youcanuseanintdatatypetostorea
wholenumber,orafloattypetostoreadecimalnumber.Butcomputerpro-
gramsoftenneedtostoreandmanipulatedataingroupsofassociatedval-
ues.Forexample,apayrollprogramkeepstrackofthenames,addresses,
socialsecuritynumbers,numberofdependents,wages,andotheritemsof
informationnecessaryformanagingthecompany'semployeedatabase.
Thisisbestaccomplishedbystoringseveraldataitems,ofthesame
type,inasinglestructure,calledan array. Forexample,thepayrollpro -
gramcanuseanarraytostoretheemployeenames,anotheronetostore
addresses,athirdonefortheemployee'ssocialsecuritynumbers,andso
on.Asyouwillseeinthischapter,dataplacedinarraysiseasytosave,re -
trieve,andprocess.
Java Arrays
Anarrayisanorderedlistofdata,allofthesametype.Apayrollprogram
canuseanarrayofJavaStringstostorethelastnamesoftheemployeesand
anotheroneofinttypetostorethenumberofdependents.Thegluethat
holdstogetherthecorrespondingelementsofseveralarraysistheorderin
whichtheyappear.Thisrelativepositionoftheelementsofanarrayis
calledthe index . Figure6-1 (onthenextpage)representsthesearraysina
company with 10 employees.
Search WWH ::




Custom Search