Chapter 7
Creating an Array
-After an 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]; -Assigns the reference of the newly created array to the variable arrayRefVar -Can also be done all in one statement: elementType[] arrayRefVar = new elementType[arraySize]; or elementType arrayRefVar[] = new elementType[arraySize]; -An array variable that appears to hold an array actually contains a reference to that array
Heap
-Arrays are objects in Java -The JVM stores the objects in an area of memory called the heap, which is used for dynamic memory allocation
Copying Arrays
-Cannot use = operator and arrayRefVar name to copy array contents ex. list2 = list1; -This statement does not copy the contents of the array referenced by list1 to list2, but instead merely copies the reference value from list1 to list2; makes both variables point to the same memory location -Instead, use a loop to copy individual elements one by one, or use the static arraycopy method in the System class
Declaring an Array
-ElementType[] arrayRefVar; -The elementType can be any data type, and all elements in the array will have the same data type -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 storage location for the reference to an array -If a variable does not contain a reference to an array, the value of the variable is null; you cannot assign elements to an array unless it has already been created
Foreach Loops
-Enables you to traverse the array sequentially without using an index variable -Syntax: for (elementType element: arrayRefVar) { // Process the element } -Note that the variable, element, must be declared as the same type as the elements in the array
Selection Sort
-Finds the smallest number in the list and swaps it with the first element; then finds the smallest number remaining and swaps it with the second element, and so on, until only a single number remains
Passing Arrays to Methods
-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, that is, the array in the method is the same as the array being passed; thus, if you change the array in the method, you will see the change outside the method -Syntax: In method parameter list: (elementType[] arrayName) Method call statement: methodName(arrayName);
Array Initializers
-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 = {value0, value1, ..., valuek}; -The new 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};
Obtaining Array Size
-Size can be obtained using arrayRefVar.length
Returning An Array From a Method
-Syntax: public static returnType elementType[] arrayName(parameter list)
Binary Search
-The elements in the array must already be ordered; assume that the array is in ascending order -The binary search first compares the key with the element in the middle of the array; if the key is less than the middle element, you need to continue to search for the key only in the first half of the array; if the key is equal to the middle element, the search ends with a match; if the key is greater than the middle element, you need to continue to search for the key only in the second half of the array -Eliminates at least half of the array after each comparison; sometimes you eliminate half of the elements, and sometimes you eliminate half plus one
The Arrays Class
-The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, filling array elements, and returning a string representation of the array; these methods are overloaded for all primitive types -You can use the sort or parallelSort method to sort a whole array or a partial array -Can use the binarySearch method to search for a key in an array; the array must be presorted in increasing order -Can use the equals method to check whether two arrays are strictly equal; two arrays are strictly equal if their corresponding elements are the same -Can use the fill method to fill in all or part of the array ex. java.util.Arrays.fill(list1, 5); // Fill 5 to the whole array java.util.Arrays.fill(list2, 1, 5, 8); // Fill 8 to a partial array into elements list2[1] through list2[5-1] -Can also use the toString method to return a string that represents all elements in the array
Linear Search
-The linear search approach compares the key element 'key' sequentially with each element in the array; it continues to do so until the key matches an element in the array or the array is exhausted without a match being found -If a match is made, the linear search returns the index of the element in the array that matches the key; if no match is found, the search returns -1. -The linear search method compares the key with each element in the array; the elements can be in any order -On average, the algorithm will have to examine half of the elements in an array before finding the key, if it exists -Since the execution time of a linear search increases linearly as the number of array elements increases, linear search is inefficient for a large array
Insertion Sort
-Works by repeatedly inserting a new element into an already sorted sublist -Given a sorted sublist, insert the next unsorted element into it, then repeat for next unsorted element and so on
Variable Length Argument Lists
-You can pass a variable number of arguments of the same type to a method -The parameter in the method is declared as follows: typeName... parameterName -In the method declaration, you specify the type followed by an ellipsis (...) -Only one variable-length parameter may be specified in a method, and this parameter must be the last parameter; any regular parameters must precede it -Java treats a variable-length parameter as an array; you can pass an array or a variable number of arguments to a variable-length parameter -When invoking a method with a variable number of arguments, Java creates an array and passes the arguments to it ex. Method calls printMax(34, 3, 3, 2, 56.5); printMax(new double[]{1, 2, 3}); Method header: public static void printMax(double... numbers)
arrayCopy Method
-arraycopy(sourceArray, srcPos, targetArray, tarPos, length); -The parameters srcPos and tarPos indicate the starting positions in sourceArray and targetArray, respectively; the number of elements copied from sourceArray to targetArray is indicated by length -Does not allocate memory space for the target array; the target array must have already been created with its memory space allocated -After the copying takes place, targetArray and sourceArray have the same content but independent memory locations