Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays, Methods, Error Handling. Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray,

Similar presentations


Presentation on theme: "Arrays, Methods, Error Handling. Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray,"— Presentation transcript:

1 Arrays, Methods, Error Handling

2 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

3 Form of Method Header ( [] ) Use [] to allow arrays of any size

4 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, 100000));

5 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));

6 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));

7 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));

8 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 -24-53 0 1 2 3 formalArray

9 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 -24-53 0 1 2 3 formalArray resultArray 2453 0 1 2 3 secondArray

10 Form of Array Return Type [] ( )

11 Copying vs Assignment int[] array1 = {3, 4, 5}; int[] array2 = array1; array1 345 0 1 2 array2 Array is a reference type. Assignment copies a pointer.

12 Copying vs Assignment int[] array1 = {3, 4, 5}; int[] array2 = array1; array1[1] = 6; array1 365 0 1 2 array2 Aliasing can cause serious side effects.

13 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]; array1 345 0 1 2 array2 345 Work with distinct array objects. Use a loop to transfer data.

14 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

15 Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; numbers 3264 0 1 2 3

16 Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; int[] tempArray = new int[numbers.length * 2]; numbers 3264 0 1 2 3 tempArray 0 1 2 3 4 5 6 7

17 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 3264 0 1 2 3 tempArray 3264 0 1 2 3 4 5 6 7

18 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 3264 0 1 2 3 tempArray 3264 0 1 2 3 4 5 6 7 Off to garbage collector

19 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

20 Assignment Creates an Alias int[] numbers = {3, 2, 6, 4}; int[] alias = numbers; // An alias int[] copy = resize(numbers, 1); // A real copy numbers 3264 0 1 2 3 alias copy 3264 0 1 2 3


Download ppt "Arrays, Methods, Error Handling. Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray,"

Similar presentations


Ads by Google