Presentation is loading. Please wait.

Presentation is loading. Please wait.

***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.

Similar presentations


Presentation on theme: "***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a."— Presentation transcript:

1 ***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a temporary variable on the memory stack. If the method variable is changed, it has no effect on the calling variable. Only the memory stack copy is changed. Recall also that, while methods can accept multiple parameters, they can only return one value. Arrays overcome this restriction. Array variables are passed to a method by reference; i.e., only a pointer to the calling array is passed, which means that any changes to the method’s array will change the calling array also. Thus, arrays permit methods to, in a sense, “return” multiple values.

2 ***** SWTJC STEM ***** Chapter 7 cg 50 Arrays Parameters Example First we will pass an array and return a single value. Create the array. int[ ] myList2 = {0, 1, 2, 3, 4}; Call the sumArray method in the class MyArrayUtility to sum the elements of the array. System.out.println("The sum of elements is " + MyArrayUtility.sumArray(myList2)); The output is: The sum of elements is 10

3 ***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b sumArray Method Calling statement (passes array myList2): System.out.println("The sum of elements is " + MyArrayUtility.sumArray(myList2)); Called method (returns single variable sum): public static int sumArray(int[] array) { int sum = 0; for (int i = 0; i < array.length; i++) sum+=array[i]; return sum; }

4 ***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Arrays as Parameters Keep in mind that a “calling” array is not passed to the memory stack; only a reference pointer is passed! Thus, the sumArray method’s array is actually myList2; i.e., changing array changes myList2. In the next example, we pass an array, manipulate it, and “return” the changed array. Consider this example: “Code a method to reverse the elements of an array; i.e., first and last elements swapped, second and next to last elements swapped, etc.” We don’t actually pass and return the array. We pass only the pointer, so in the called method, we are actually manipulating the original array!

5 ***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Reverse Array Method Create the array. int[ ] myList2 = {0, 1, 2, 3, 4}; Call the reverseArray method in the class MyArrayUtility to reverse the elements of the array. MyArrayUtility.reverseArray(myList2));........ The value of array element 0 is 4 The value of array element 1 is 3 The value of array element 2 is 2 The value of array element 3 is 1 The value of array element 4 is 0

6 ***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Swapping Variables Temp = A A = B B = Temp

7 ***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Reverse Array Method public static void reverseArray(int[] array) { int temp, j = array.length - 1; for (int i = 0; i < array.length / 2; i++) { temp = array[i]; array[i] = array[j]; array[j] = temp; j--; } 0(4) 1 2 3 4(0) 0 i = 0 j = 4 temp

8 ***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Reverse Array Method public static void reverseArray(int[] array) { int temp, j = array.length - 1; for (int i = 0; i < array.length / 2; i++) { temp = array[i]; array[i] = array[j]; array[j] = temp; j--; } 4 1(3) 2 3(1) 0 1 i = 1 j = 3 temp

9 ***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Reverse Array Method public static void reverseArray(int[] array) { int temp, j = array.length - 1; for (int i = 0; i < array.length / 2; i++) { temp = array[i]; array[i] = array[j]; array[j] = temp; j--; } 4 3 2 1 0 i = 2 j = 4 temp

10 ***** SWTJC STEM ***** Chapter 7 cg 51 Chapter 7 Part b Copying Arrays To copy a single variable, we use the assignment statement. heightCopy = height; // Works nicely. But does this work with arrays? Consider array myList: myListCopy = myList; // Not exactly! Array assignments copy the pointer only, which means that myListCopy is really pointing to the same array data as myList. Changing myListCopy will change myList. Thus, it is not an independent copy! To create an independent copy, use a for loop or the static arraycopy method of the System class.

11 ***** SWTJC STEM ***** Chapter 7 cg 51 Chapter 7 Part b Array Copy Example int myList1[] = {0, 1, 2, 3, 4}; int myList2[]; myList2 = myList1; // Attempt to copy myList1 to myList2..... myList2[3] = 7; // Change element 3 of myList2 Did not Work!!!! Why? index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 3 index 4 myList1 4 myList2 4 Before index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 7 myList2 7 index 4 myList1 4 myList2 4 After

12 ***** SWTJC STEM ***** Chapter 7 cg 51 Chapter 7 Part b Array Copy For Loop int myList1[] = {0, 1, 2, 3, 4}; int[] myList2 = new int[myList1.length]; for(int i = 0; i < myList1.length; i++) myList2[i] = myList1[i];..... myList2[3] = 7; // Change only myList2???? Worked OK!!!! index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 3 index 4 myList1 4 myList2 4 Before index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 7 index 4 myList1 4 myList2 4 After

13 ***** SWTJC STEM ***** Chapter 7 cg 51 Chapter 7 Part b Array Copy System int myList1[] = {0, 1, 2, 3, 4}; int[] myList2 = new int[myList1.length]; System.arraycopy(myList1, 0, myList2, 0, myList1.length);..... myList2[3] = 7; // Change only myList2???? Worked OK!!!! index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 7 index 4 myList1 4 myList2 4 After index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 3 index 4 myList1 4 myList2 4 Before


Download ppt "***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a."

Similar presentations


Ads by Google