20
Arrays, Methods, Error Handling

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

Embed Size (px)

Citation preview

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

Arrays, Methods, Error Handling

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

Using Arrays as Parametersint[] anArray = <whatever>;

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

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

Form of Method Header<return type> <method name> (<base type>[] <param name>)

Use [] to allow arrays of any size

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

Example: Linear Searchint 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));

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

Use Logical Sizeint 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));

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

Example: Return a Sumint 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));

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

Build a String from an ArrayString 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 canbe used with numbers or strings.

String[] array = {"Hi", " there!"};

System.out.println(sum(array, array1.length));

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

Modifying an Array Parametervoid 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 -2 4 -5 3 0 1 2 3

formalArray

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

Returning an Array Valueint[] 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 -2 4 -5 3 0 1 2 3

formalArray

resultArray 2 4 5 3 0 1 2 3

secondArray

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

Form of Array Return Type<base type>[] <method name>(<parameters>)

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

Copying vs Assignmentint[] array1 = {3, 4, 5};

int[] array2 = array1;

array1 3 4 5 0 1 2

array2

Array is a reference type.Assignment copies a pointer.

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

Copying vs Assignmentint[] array1 = {3, 4, 5};

int[] array2 = array1;

array1[1] = 6;

array1 3 6 5 0 1 2

array2

Aliasing can cause serious side effects.

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

Copying vs Assignmentint[] array1 = {3, 4, 5};

int[] array2 = new int[3];

for (int i = 0; i < array2.length; i++) array2[i] = array1[i];

array1 3 4 5 0 1 2

array2 3 4 5

Work with distinct array objects.Use a loop to transfer data.

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

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

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

Make the Array Twice as Largeint[] numbers = {3, 2, 6, 4};

numbers 3 2 6 4 0 1 2 3

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

Make the Array Twice as Largeint[] numbers = {3, 2, 6, 4};

int[] tempArray = new int[numbers.length * 2];

numbers 3 2 6 4 0 1 2 3

tempArray

0 1 2 3 4 5 6 7

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

Make the Array Twice as Largeint[] 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 3 2 6 4 0 1 2 3

tempArray 3 2 6 4 0 1 2 3 4 5 6 7

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

Make the Array Twice as Largeint[] 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 3 2 6 4 0 1 2 3

tempArray 3 2 6 4 0 1 2 3 4 5 6 7

Off to garbage collector

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

A General Methodint[] 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

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

Assignment Creates an Alias

int[] numbers = {3, 2, 6, 4};

int[] alias = numbers; // An alias

int[] copy = resize(numbers, 1); // A real copy

numbers 3 2 6 4 0 1 2 3

alias

copy 3 2 6 4 0 1 2 3