7.6. Code Practice with Arrays¶
Fix the following code so that it prints every other value in the array arr1
starting with the value at index 0.
Change line 5 to add the []
on the declaration of arr1
to show that it is an array of integer values. Change line 6 to index < arr1.length
so that you don’t go out of bounds (the last valid index is the length minus one). Change line 8 to print arr1[index]
.
Solution to question above.
Fix the following to print the values in the array a1
starting with the value at the last index and then backwards to the value at the first index.
Rewrite the following code so that it prints all the values in an array arr1
using a for-each loop instead of a for
loop.
Finish the following code so that it prints out all of the odd values in the array a1
.
Finish the following method getSum
to return the sum of all values in the passed array.
Finish the following method to return the sum of all of the non-negative values in the passed array.
Finish the following code to print the strings at the odd indices in the array.
Finish the method getSumChars
below to return the total number of characters in the array of strings strArr
.
Finish the method findMin
so that it finds and returns the minimum value in the array.
Finish the method getAverage
to calculate and return the average of all of the values in the array.
7.6.1. More Practice¶
For practice with simple array manipulation and conditionals, but no loops see http://codingbat.com/java/Array-1. For more practice with loops and arrays go to http://codingbat.com/java/Array-2.
Here are problems without loops
Here are problems with loops