7.8. 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.
Change line 6 to a1.length - 1
since the last valid index is one less than the length of the array and i >= 0
since the first valid index is 0. Change line 7 to a1
.
Solution to question above.
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.
In a for-each loop you specify the type of the values in the array, a name for the current value, and then a :
and then the name of the array. The first time through the loop the value will be the one at index 0. The next time the one at index 1 and so on until you reach the last value in the array.
Solution to question above.
Finish the following code so that it prints out all of the odd values in the array a1
. Hint: use % to check for odd values.
If the remainder of the value divided by 2 is 1 then it is odd so print it out followed by a space (to keep the values separated).
Solution to question above.
Finish the following method getSum
to return the sum of all values in the passed array.
Declare a variable to hold the sum
and initialize it to zero. Loop through all the values in the array using a for-each loop and add each value to the sum
. Return the sum
.
Solution to question above.
Finish the following method to return the sum of all of the non-negative values in the passed array.
Declare a variable to hold the sum
and initialize it to zero. Loop through all the values in the array. If the current value is non negative (greater than or equal to 0) then add it to the sum
. Return the sum
.
Solution to question above.
Finish the following code to print the strings at the odd indices in the array.
Use a for loop and start the index at 1 and increment it by 2 each time through the loop. Print the value at the index.
Solution to question above.
Finish the method getSumChars
below to return the total number of characters in the array of strings strArr
.
Declare the sum
and initialize it to 0. Use a for-each loop to loop through each string in the array. Add the length of the current string to the sum
. Return the sum
.
Solution to question above.
Finish the method findMin
so that it finds and returns the minimum value in the array.
Declare a variable to hold the minimum value found and initialize it to the first value in the array. Loop from 1 to the length of the array minus one and get the value at that index. If the value is less than the minimum found so far reset the minimum found so far to the value. Return the minimum.
Solution to question above.
Finish the method getAverage
to calculate and return the average of all of the values in the array.
Declare a variable to hold the total
and it should be of type double
so that the average is a double
. Initialize it to 0. Loop through all the values in the array and add each to the total
. Return the total
divided by the length of the array.
Solution to question above.
7.8.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