Show 1 Question 1 [Multiple Choice] [10] 1.After you create an array variable, you still need to ________ memory space. a. Create b.Organize c. Reserve d.Dump 2.A (n) ____ is an integer contained within square brackets that indicates one of an array’s variables. a. Item b.Subscript c. Postscript d.Indicator 3.In Java, boolean array elements automatically are assigned the value a. Null b. ‘\u0000’ c. True d.False 4.Providing values for all the elements in an array is called ____ the array. a. Initializing b.Filling c. Populating d.Declaring. 5.The length ____ contains the number of elements in the array. a. Item b.Field c. Area d.Block 6.A ____ array is one with the same number of elements as another, and for which the values in corresponding elements are related. a. Cloned b.Parallel c. Property d.Two-dimensional 7.When a method returns an array reference, you include ____ with the return type in the method header. a.() b.// c.{} d.[] 8.Which of the following statements correctly initializes an array with an initialization list? a. int[] nums = {2, 4, 8}; b. int[] nums = (2, 4, 8); c. int nums = [2, 4, 8]; d. int nums() = int{2, 4, 8}; 9.In which of the following statements is the value of myVals null? a. int myVals = "" b. int [] myVals; c. myVals = int[null] d. int[null] = myVals An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the An array of 10 elements. Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8. The following program, class ArrayDemo { public static void main(String[] args) { // declares an array of integers int[] anArray; // allocates memory for 10 integers anArray = new int[10]; // initialize first element anArray[0] = 100; // initialize second element anArray[1] = 200; // and so forth anArray[2] = 300; anArray[3] = 400; anArray[4] = 500; anArray[5] = 600; anArray[6] = 700; anArray[7] = 800; anArray[8] = 900; anArray[9] = 1000; System.out.println("Element at index 0: " + anArray[0]); System.out.println("Element at index 1: " + anArray[1]); System.out.println("Element at index 2: " + anArray[2]); System.out.println("Element at index 3: " + anArray[3]); System.out.println("Element at index 4: " + anArray[4]); System.out.println("Element at index 5: " + anArray[5]); System.out.println("Element at index 6: " + anArray[6]); System.out.println("Element at index 7: " + anArray[7]); System.out.println("Element at index 8: " + anArray[8]); System.out.println("Element at index 9: " + anArray[9]); } } The output from this program is: Element at index 0: 100 Element at index 1: 200 Element at index 2: 300 Element at index 3: 400 Element at index 4: 500 Element at index 5: 600 Element at index 6: 700 Element at index 7: 800 Element at index 8: 900 Element at index 9: 1000 In a real-world programming situation, you would probably
use one of the supported looping constructs to iterate through each element of the array, rather than write each line individually as in the preceding example. However, the example clearly illustrates the array syntax. You will learn about the various looping constructs ( Declaring a Variable to Refer to an ArrayThe
preceding program declares an array (named // declares an array of integers int[] anArray; Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as Similarly, you can declare arrays of other types: byte[] anArrayOfBytes; short[] anArrayOfShorts; long[] anArrayOfLongs; float[] anArrayOfFloats; double[] anArrayOfDoubles; boolean[] anArrayOfBooleans; char[] anArrayOfChars; String[] anArrayOfStrings; You can also place the brackets after the array's name: // this form is discouraged float anArrayOfFloats[]; However, convention discourages this form; the brackets identify the array type and should appear with the type designation. Creating, Initializing, and Accessing an ArrayOne way to create an array is with the // create an array of integers anArray = new int[10]; If this statement is missing, then the compiler prints an error like the following, and compilation fails: ArrayDemo.java:4: Variable anArray may not have been initialized. The next few lines assign values to each element of the array: anArray[0] = 100; // initialize first element anArray[1] = 200; // initialize second element anArray[2] = 300; // and so forth Each array element is accessed by its numerical index: System.out.println("Element 1 at index 0: " + anArray[0]); System.out.println("Element 2 at index 1: " + anArray[1]); System.out.println("Element 3 at index 2: " + anArray[2]); Alternatively, you can use the shortcut syntax to create and initialize an array: int[] anArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 }; Here the length of the array is determined by the number of values provided between braces and separated by commas. You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as In the Java programming language, a multidimensional array is an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length, as
shown in the following class MultiDimArrayDemo { public static void main(String[] args) { String[][] names = { {"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"} }; // Mr. Smith System.out.println(names[0][0] + names[1][0]); // Ms. Jones System.out.println(names[0][2] + names[1][1]); } } The output from this program is: Finally, you can use the built-in System.out.println(anArray.length); Copying ArraysThe public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) The two The following program,
class ArrayCopyDemo { public static void main(String[] args) { String[] copyFrom = { "Affogato", "Americano", "Cappuccino", "Corretto", "Cortado", "Doppio", "Espresso", "Frappucino", "Freddo", "Lungo", "Macchiato", "Marocchino", "Ristretto" }; String[] copyTo = new String[7]; System.arraycopy(copyFrom, 2, copyTo, 0, 7); for (String coffee : copyTo) { System.out.print(coffee + " "); } } } The output from this program is: Cappuccino Corretto Cortado Doppio Espresso Frappucino Freddo Array ManipulationsArrays are a powerful and useful concept
used in programming. Java SE provides methods to perform some of the most common manipulations related to arrays. For instance, the For your convenience, Java SE provides several methods for performing array manipulations (common tasks, such as copying, sorting and searching arrays) in the class ArrayCopyOfDemo { public static void main(String[] args) { String[] copyFrom = { "Affogato", "Americano", "Cappuccino", "Corretto", "Cortado", "Doppio", "Espresso", "Frappucino", "Freddo", "Lungo", "Macchiato", "Marocchino", "Ristretto" }; String[] copyTo = java.util.Arrays.copyOfRange(copyFrom, 2, 9); for (String coffee : copyTo) { System.out.print(coffee + " "); } } } As you can see, the output from
this program is the same, although it requires fewer lines of code. Note that the second parameter of the Some other useful operations provided by methods in the
|