很基础的问题,不容得出错;出错只能说明一个问题:基础不牢固。
Self-Review Exercises 7.1 Fill in the blank(s) in each of the following statements: a) Lists and tables of values can be stored in arrays and collections. b) An array is a group of variables (called elements or components) containing values that all have the same type. c) The enhanced for statement allows you to iterate through an array’s elements without using a counter. d) The number used to refer to a particular array element is called the element’s index (or subscript or position number). e) An array that uses two indices is referred to as a(n) two-dimensional array. f) Use the enhanced for statement for (double d: Numbers) to walk through double array numbers. g) Command-line arguments are stored in array of Strings called args by convention. h) Use the expression args.length to receive the total number of arguments in a command line. Assume that command-line arguments are stored in String[] args. i) Given the command java MyClass test, the first command-line argument is test. j) A(n) ellipsis (…) in the parameter list of a method indicates that the method can receive a variable number of arguments. 7.2 Determine whether each of the following is true or false. If false, explain why. a) An array can store many different types of values. (F) b) An array index should normally be of type float. (F) c) An individual array element that’s passed to a method and modified in that method will contain the modified value when the called method completes execution. (F) d) Command-line arguments are separated by commas. (F) 7.3 Perform the following tasks for an array called fractions: a) Declare a constant ARRAY_SIZE that’s initialized to 10. (final int ARRAY_SIZE = 10; ) b) Declare an array with ARRAY_SIZE elements of type double, and initialize the elements to 0. (double[] fractions = new array[ARRAY_SIZE]; ) c) Refer to array element 4. (fractions[4]) d) Assign the value 1.667 to array element 9. (fractions[9] = 1.667) e) Assign the value 3.333 to array element 6. (fractions[6] = 3.333) f) Sum all the elements of the array, using a for statement. Declare the integer variable x as a control variable for the loop. ( double sum = 0.0; for (int x = 0; x < fractions.length; x++) total += fractions[x]; ) 7.4 Perform the following tasks for an array called table: a) Declare and create the array as an integer array that has three rows and three columns. Assume that the constant ARRAY_SIZE has been declared to be 3. ( final int ARRAY_SIZE = 3; int[][] table = new array[ARRAY_SIZE][ ARRAY_SIZE]; ) b) How many elements does the array contain? (3*3=9) c) Use a for statement to initialize each element of the array to the sum of its indices. Assume that the integer variables x and y are declared as control variables. ( for (int x = 0; x < ARRAY_SIZE; x++) for (int y = 0; y < ARRAY_SIZE; y++) { table[x][y] = x+y; } )
7.5 Find and correct the error in each of the following program segments: a) final int ARRAY_SIZE = 5; ARRAY_SIZE = 10; b) Assume int[] b = new int[10]; for (int i = 0; i <= b.length; i++) b[i] = 1; c) Assume int[][] a = {{1, 2}, {3, 4}}; a[1, 1] = 5;
|