Accessing Arrays Tutorial

From Progzoo
(Redirected from Accessing array values)
Jump to: navigation, search

In these questions you must complete the method doStuff.

The system will call your method three times with a different array each time.

Your method should give the output specified each time.

Elements 1 and 3

Print element 1 and element 3 of the array x

For the array [2, 7, 5, 3] you should print 7 and 3

Element 0 is 2, element 1 is 7, element 2 is 5, element 3 is 3.


[Font] [Default] [Show] [Resize] [History] [Profile]

Add the first two

Print element 0 add element 1 of the array x

Element zero is x[0], element one is x[1]. Add these two values and print the result.

For the array [2, 7, 5, 3] you should print 9 (because that is 2+7).


[Font] [Default] [Show] [Resize] [History] [Profile]

Show the last element

Print the last item of the array.

Notice that the last index is x.length-1

For the array [2, 7, 5, 3] you should print 3


[Font] [Default] [Show] [Resize] [History] [Profile]

Add the first and the last element

Print the first item plus the last item of the array.

For the array [2, 7, 5, 3] you should print 5 (because that is 2+3)


[Font] [Default] [Show] [Resize] [History] [Profile]