Arrays
The preceding statement creates an array using the following syntax: new elementType[]{value0, value1, ..., valuek}; There is no explicit reference variable for the array. Such array is called an __.
anonymous array
Java has a shorthand notation, known as the _, which combines the declaration, creation, and initialization of an array in one statement.
array initializer
Once array variable is declared, you can create an array by using the new operator and assign its reference to the variable with the following syntax:
arrayRefVar = new elementType[arraySize];
When space for an array is allocated, the array size must be given, specifying the number of ele-ments that can be stored in it. The size of an array cannot be changed after the array is created. Size can be obtained using ___
arrayRefVar.length (For example, myList.length is 10)
The array elements are accessed through the index. Array indices are _; that is, they range from 0 to arrayRefVar.length-1.
arrayRefVar.length-1
To assign values to the elements, use the syntax:
arrayRefVar[index] = value; (For example, the following code initializes the array. myList[0] = 5.6; myList[1] = 4.5;)
Each element in the array is represented using the following syntax, known as an indexed variable:
arrayRefVar[index];
There are three ways to copy arrays: ■ Use a loop to copy individual elements one by one. ■ Use the static __ in the System class. ■ Use the clone method to copy arrays;
arraycopy method
Another approach is to use the arraycopy method in the java.lang.System class to copy arrays instead of using a loop. The syntax for arraycopy is:
arraycopy(sourceArray, srcPos, targetArray, tarPos, length);
For an array of the char[] type, it can be printed using _ statement. For exam-ple, the following code displays Dallas: char[] city = {'D', 'a', 'l', 'l', 'a', 's'}; System.out.println(city);
one print
■ For an argument of a primitive type, the argument's value is passed. ■ For an argument of an array type, the value of the argument is a reference to an array; this reference value is passed to the method. Semantically, it can be best described as ___
pass-by-sharing
You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3,1,2.
printArray(new int[]{3, 1, 2});
Once an array is created, its size is _. An array reference variable is used to access the elements in an array using an _.
Fixed, index
If a variable does not contain a reference to an array, the value of the variable _. You cannot assign elements to an array unless it has already been created.
Is null
The _ operator is not used in the array-initializer syntax. Using an array initializer, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. Thus, the next statement is wrong: double[] myList; myList = {1.9, 2.9, 3.4, 3.5};
New
There are three ways to copy arrays: ■ Use a loop to copy individual elements one by one. ■ Use the static arraycopy method in the System class. ■ Use the __ to copy arrays
clone method
Initializing arrays with random values: The following loop initializes the array myList with random values between 0.0 and 100.0, but less than 100.0.
for (int i = 0; i < myList.length; i++) { myList[i] = Math.random() * 100; }
The following loop assigns 0 to myList[0],1 to myList[1], . . . , and 9 to myList[9]:
for (int i = 0; i < myList.length; i++) { myList[i] = i; }
Unlike declarations for primitive data type variables, the declaration of an array variable does not allocate any space in memory for the array. It creates only a _ for the refer-ence to an array.
storage location
Displaying arrays: To print an array, you have to print each element in the array using a loop like the following: for (int i = 0; i < myList.length; i++) { ____
System.out.print(myList[i] + " "); }
This statement declares an array variable, myList, creates an array of ten elements of double type, and assigns its reference to myList.
double[] myList = new double[10];
When an array is created, its elements are assigned the default value of _ for the numeric primitive data types, _ for char types, and _ for boolean types.
0, \u0000, false
Accessing an array out of bounds is a common programming error that throws a runtime ___. To avoid it, make sure that you do not use an index beyond arrayRefVar.length - 1.
ArrayIndexOutOfBoundsException
Arrays are _ in Java. The JVM stores the objects in an area of memory called the _, which is used for _ memory allocation.
Objects, heap, dynamic
There are three ways to copy arrays: ■ ____. ■ Use the static arraycopy method in the System class. ■ Use the clone method to copy arrays
Use a loop (to copy individual elements one by one)
Thus it is all right to say, for simplicity, that myList is an array, instead of stating, at greater length, that myList is a _ that contains a reference to an array of ten double elements.
Variable
Array initializer: _ _ & _
declares, creates, initializes
Array initializer: declares, creates, and initializes the array myList with four elements, which is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9;
double[] myList = {1.9, 2.9};
The _ can be any data type, and all elements in the array will have the _ data type. For example, the following code declares a variable myList that references an array of double elements. __
elementType, same, double[] myList;
Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement as:
elementType[] arrayRefVar = new elementType[arraySize];
Java has a shorthand notation, known as the array initializer, which combines the declaration, creation, and initialization of an array in one statement using the following syntax:
elementType[] arrayRefVar = {v1, vk};
Syntax for declaring an array variabl
elementType[] arrayRefVar;
Java supports a convenient for loop, known as a foreach loop, which enables you to traverse the array sequentially without using an index variable. For example, You can read the code as "for each element e in myList, do the following. _____ { System.out.println(e); }
for (double e: myList)
The array previously referenced by list2 is no longer referenced; it becomes garbage, which will be automatically collected by the Java Virtual Machine (this process is called __).
garbage collection
Finding the largest element: Use a variable named max to store the largest element. Initiallymax is myList[0]. To find the largest element in the array myList, compare each element with max, and update max if the element is greater than max. double max = myList[0]; for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i]; }
After the copying takes place, targetArray and sourceArray have the same content but _ memory location
independent
// Generate an index j randomly with 0 <= j <= i
int j = (int)(Math.random() * (i + 1));
In Java, you can use assignment statements to copy primitive data type variables, but not arrays. Assigning one array variable to another array variable actually copies one reference to another and makes both variables point to the same ___.
memory location
The following code prompts the user to enter a month number and displays its month name: String[] months = {"January", "February", ..., "December"}; System.out.print("Enter a month number (1 to 12): "); int monthNumber = input.nextInt(); System.out.println("The month is " +
months[monthNumber - 1]);
Initializing arrays with input values: The following loop initializes the array myList with user input values. java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter " + myList.length + " values: "); for (int i = 0; i < myList.length; i++) ___
myList[i] = input.nextDouble(); }
Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array: 1st line
public static void printArray(int[] array) {