Arrays, Methods, Error Handling
Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray, aTarget); // Index of target int s = sum(anArray); // Sum of data abs(anArray); // Convert items to // absolute values
Form of Method Header ( [] ) Use [] to allow arrays of any size
Example: Linear Search int search (int[] array, int target){ // Returns index of first target found, or -1 otherwise. for (int i = 0; i < array.length; i++) if (array[i] == target) return i; return -1; } int[] array1 = {45, 67, 100, 22, 88}; System.out.println(search(array1, 100)); int[] array2 = {45, 67, 100, 22, 88, 55, 99}; System.out.println(search(array2, ));
Use Logical Size int search (int[] array, int target, int logicalSize){ // Returns index of first target found, or -1 otherwise. for (int i = 0; i < logicalSize; i++) if (array[i] == target) return i; return -1; } int[] array1 = {45, 67, 100, 22, 88}; System.out.println(search(array1, 100, array1.length));
Example: Return a Sum int sum (int[] array, int logicalSize){ int result = 0; for (int i = 0; i < logicalSize; i++) result = result + array[i]; return result; } int[] array1 = {2, 2, 2, 2, 2}; System.out.println(sum(array1, array1.length)); int[] array2 = {3, 4}; System.out.println(sum(array2, array2.length));
Build a String from an Array String sum (String[] array, int logicalSize){ String result = ""; for (int i = 0; i < logicalSize; i++) result = result + array[i]; return result; } Accumulation is a common pattern that can be used with numbers or strings. String[] array = {"Hi", " there!"}; System.out.println(sum(array, array1.length));
Modifying an Array Parameter void abs(int[] formalArray){ // Converts each integer to its absolute value for (int i = 0; i < formalArray.length; i++) formalArray[i] = Math.abs(formalArray[i]); } int[] actualArray = {-2, 4, -5, 3}; abs(actualArray); actualArray formalArray
Returning an Array Value int[] abs(int[] formalArray){ int[] resultArray = new int[formalArray.length]; for (int i = 0; i < formalArray.length; i++) resultArray[i] = Math.abs(formalArray[i]); return resultArray; } int[] actualArray = {-2, 4, -5, 3}; int[] secondArray = abs(actualArray); actualArray formalArray resultArray secondArray
Form of Array Return Type [] ( )
Copying vs Assignment int[] array1 = {3, 4, 5}; int[] array2 = array1; array array2 Array is a reference type. Assignment copies a pointer.
Copying vs Assignment int[] array1 = {3, 4, 5}; int[] array2 = array1; array1[1] = 6; array array2 Aliasing can cause serious side effects.
Copying vs Assignment int[] array1 = {3, 4, 5}; int[] array2 = new int[3]; for (int i = 0; i < array2.length; i++) array2[i] = array1[i]; array array2 345 Work with distinct array objects. Use a loop to transfer data.
Resizing an Array Java arrays cannot be resized But we can –create a larger or smaller new array –transfer data from the original array to the new array –reset the original array variable to the new array
Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; numbers
Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; int[] tempArray = new int[numbers.length * 2]; numbers tempArray
Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; int[] tempArray = new int[numbers.length * 2]; for (int i = 0; i < numbers.length; i++) tempArray[i] = numbers[i]; numbers tempArray
Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; int[] tempArray = new int[numbers.length * 2]; for (int i = 0; i< numbers.length; i++) tempArray[i] = numbers[i]; numbers = tempArray; numbers tempArray Off to garbage collector
A General Method int[] resize(int[] anArray, double sizeFactor){ int[] tempArray = new int[(int)(anArray.length * sizeFactor)]; for (int i = 0; i< anArray.length; i++) tempArray[i] = anArray[i]; return tempArray; } int[] numbers = {3, 2, 6, 4}; numbers = resize(numbers, 2); // Double the length int[] copy = resize(numbers, 1); // Just a copy
Assignment Creates an Alias int[] numbers = {3, 2, 6, 4}; int[] alias = numbers; // An alias int[] copy = resize(numbers, 1); // A real copy numbers alias copy