Upload
pauline-greer
View
217
Download
0
Embed Size (px)
Citation preview
Arrays, Methods, Error Handling
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
Form of Method Header<return type> <method name> (<base type>[] <param name>)
Use [] to allow arrays of any size
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));
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));
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));
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));
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
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
Form of Array Return Type<base type>[] <method name>(<parameters>)
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.
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.
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.
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 Largeint[] numbers = {3, 2, 6, 4};
numbers 3 2 6 4 0 1 2 3
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
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
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
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
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