7.2 Processing Array Elements & 7.3 Passing Arrays as Arguments to Methods
enhanced for loop
A special type of for loop that traverses an array or array list from beginning to end, binding a local variable to each element in the array or list.
The following statements declare two arrays. Write a for loop that copies the a array to the b array. int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = new int[7];
for(int i=0; i<a.length; ++i) {b[i] = a[i];}<br>
A program uses a variable named intarray that is of type int. You do not know the number of elements in the array. Write a for loop that stores -1 in each element of the array. ____ { intarray[i] = -1; } Fill in the blank.
for(int i=0; i<intarray.length; ++i)
Write the definition of a method named sumArray that has one parameter, an array of ints.The method returns the sum of the elements of the array as an int.
int sumArray(int a[]) { int i, total = 0; for(i = 0; i < a.length; i++) total += a[i]; return total; }
Consider the following method header: public static void myMethod(double[] array) Now suppose a program has this array declaration: double[] numbers = new double[100]; Write a statement that calls the myMethod method with the numbers array as an argument.
myMethod(numbers);<br>
reference copy
only the address of the array object is copied, not the contents of the array object
printArray is a method that accepts one argument, an array of ints. The method prints the contents of the array; it does not return a value. inventory is an array of ints that has been already declared and filled with values. Write a statement that prints the contents of the array inventory by calling the method printArray.
printArray(inventory);
Write a method named initZero that accepts an int array as an argument and stores the value 0 in each array element. public class InitZero { public static void initZero(/*____*/) { /*_______*/ } }
public class InitZero { public static void initZero(int[] array) { for (int i = 0; i < array.length; i++) array[i] = 0; } }
Based on the following declaration statements, write a statement that multiplies element 0 of the numbers1 array by element 3 of the numbers2 array and assigns the result to the result variable. int[] numbers1 = { 1, 3, 6, 9 }; int[] numbers2 = { 2, 4, 6, 8 }; int result;
result = numbers1[0] * numbers2[3];
A program has the following declarations and prompt (assume any required import statements have been included). Scanner keyboard = new Scanner(System.in); double[] anarray; int size; System.out.print("Enter the size of the array: "); Write a statement that creates an array anarray of size size.
tbd
True or False: When an array is passed to a method, the method has access to the original array.
true
Write the definition of a method printArray, which has one parameter, an array of ints.The method does not return a value. The method prints out each element of the array, on a line by itself, in the order the elements appear in the array, and does not print anything else.
void printArray(int a[]) { int i; for (i = 0; i < a.length; i++) { System.out.print(a[i]); System.out.println(); } }