Вы находитесь на странице: 1из 5

Introduction to Arrays Worksheet:

Name

Exercise 10.1 Conceptual O er ie! 1. A pro"ram needs many aria#les to store and process data. $o! does an array sol e this pro#lem%

&. $o! does the pro"rammer access an item in an array%

'. (ary is usin" an array o) dou#les to store an employee*s !a"e amounts )or each day o) the !eek +(onday throu"h ,riday-. .ra! a picture o) this array !ith sample items and re)erences to each one.

Exercise 10.& /imple Array (anipulations 1. Assume that the array contains the )i e inte"ers '01 &'1 231 451 and 1&. Write the alues o) the )ollo!in" expressions:

a.

a[1]

b.

a[a.length 1]

c.

a[2] + a[3]

&. What happens !hen a pro"ram attempts to access an item at an index that is less than 0 or "reater than or e6ual to the array*s len"th%

Exercise 10.' 7oopin" 8hrou"h Arrays 1. Write a loop that prints all o) the items in an array a to the terminal screen.

&. 9epeat :uestion 1 #ut print the items in re erse order.

'. Write a loop that locates the )irst occurrence o) a ne"ati e inte"er in an array a. When the loop is )inished1 the aria#le index should contain the index o) the ne"ati e num#er or the len"th o) the array i) there !ere no ne"ati e num#ers in the array.

0. .escri#e !hat the )ollo!in" code se"ments do: a.


for (int i = 0; i < a.length; i++) a[i] = Math.ab (a[i]);

#.
!tring tr = "#;

for (int i = 0; i < a.length; i++) tr += a[i];

;. What is the ad anta"e o) usin" the instance aria#le length in a loop !ith an array%

Exercise 10.0 .eclarin" Arrays 1. .eclare and instantiate array aria#les )or the )ollo!in" data:

a. An array o) 1; dou#les

#. An array o) &0 strin"s

&. What is an initiali<e list%

'.

=se an initiali<e list to create the )ollo!in" arrays:

a. )i e test scores o) 1001 501 3;1 201 and 44

#. three interest rates o) 0.1&1 0.0;1 and 0.1;

c. t!o strin"s1 your )irst name and last name

0. Why is it #etter to use the )orm <t$%e&[] <'ariable& instead of <t$%e& <'ariable&[] !hen declarin" an array aria#le%

Exercise 10.; Workin" !ith Arrays that are not )ull 1. What happens !hen the pro"rammer tries to access an array cell !hose index is "reater than or e6ual to its lo"ical si<e%

&. .escri#e an application that uses an array that mi"ht not #e )ull.

Exercise 10.2 >arallel Arrays 1. What are parallel arrays%

&. .escri#e an application in !hich parallel arrays mi"ht #e used.

'. .eclare and instantiate the aria#les )or parallel arrays to track the names1 a"es1 and /ocial /ecurity num#ers o) ;0 employees.

0. Assume that the array na(e contains the names o) people in a phone #ook and the parallel array n)(ber contains their phone num#ers. Write a code se"ment that displays each name and num#er in )ormatted columns +usin" ta# spacin"-. Assume that each num#er is the same len"th.

;. Write a code se"ment that creates parallel arrays containin" the )irst 10 non?ne"ati e po!ers o) &. One array should contain the exponent and the other array should contain & raised to that po!er.

Exercise 10.3 =sin" the Enhanced )or 7oop 1. Assume that array abc is )illed !ith strin"s. Con ert the )ollo!in" loops to simpler ersions usin" the enhanced )or loop. a.
for (int i = 0; i < abc.length; i++) !$ te(.o)t.%rintln(abc[i]);

#.
!tring target = ...; boolean fo)nd = fal e; for (int i = 0; i < abc.length; i++) if (target.e*)al (abc[i]))+ fo)nd = tr)e; brea,; -

&. 7ist t!o di))erent pro#lems )or !hich an enhanced )or loop !ould not #e appropriate.

Exercise 10.4 Arrays and (ethods 1. What happens !hen one uses the assi"nment operator +@- !ith t!o array aria#les%

&. .iscuss the issues in ol ed !ith copyin" an array.

'. Write a method that returns the a era"e o) the num#ers in an array o) do)ble.

0. Write a method )b.rra$ that expects an array o) int and t!o int as parameters. 8he inte"ers represent the startin" position and the endin" position o) a su#array !ithin the parameter array. 8he method should return a ne! array that contains the elements )rom the startin" position to the endin" position.

;. Write a method that searches a t!o?dimensional array )or a "i en inte"er. 8his method should return an o#Aect o) class /oint1 !hich contains a ro! and a column. 8he constructor )or /oint is /oint(an0nteger1 an0nteger).

Exercise 10.5 Array o) O#Aects 1. Write a method get2igh!t)dent that expects an array o) students as a parameter. 8he method returns the !t)dent o#Aect that contains the hi"hest score. Bou may assume that the !t)dent class has a method get2igh!core(). +$int: 8he method should declare a local aria#le o) type !t)dent to track the student !ith the hi"hest score. 8he initial alue o) this aria#le should #e n)ll.-

&. What happens !hen the )ollo!in" code se"ment is executed%


33 4eclare and re er'e 10 cell for t)dent ob5ect !t)dent[] t)dent.rra$ = ne6 !t)dent[10]; 33 .dd 7 t)dent to the arra$ for (int i = 0; i < 7; i++) t)dent.rra$[i] = ne6 !t)dent ("!t)dent " + i1 80+i1 90+i1 :0+i); 33 /rint the na(e of the t)dent for (int i = 0; i < t)dent.rra$.length; i++) !$ te(.o)t.%rintln(!t)dent.rra$[i].get;a(e());

Вам также может понравиться