ChapterSixPartII

ChapterSixPartII - IT108 Arrays Part II Passing Arrays to...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: IT108 Arrays Part II Passing Arrays to Methods public static void printArray(int array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } Invoke the method int list = {3, 1, 2, 6, 4, 2}; printArray(list); Invoke the method printArray(new int{3, 1, 2, 6, 4, 2}); Anonymous array Pass By Value • Java uses pass by value to pass parameters to a method. There are important differences between passing a value of variables of primitive data types and passing arrays. • For a parameter of a primitive type value, the actual value is passed. Changing the value of the local parameter inside the method does not affect the value of the variable outside the method. • For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method. Any changes to the array that occur inside the method body will affect the original array that was passed as the argument . public class Test { public static void main(String args) { int x = 1; // x represents an int value int y = new int[10]; // y represents an array of int values m(x, y); // Invoke m with arguments x and y System.out.println("x is " + x); System.out.println("y[0] is " + y[0]); } public static void m(int number, int numbers) { number = 1001; // Assign a new value to number numbers[0] = 5555; // Assign a new value to numbers[0] } } Simple Example Call Stack When invoking m(x, y) , the values of x and y are passed to number and numbers . Since y contains the reference value to the array, numbers now contains the same reference value to the same array. Space required for the main method int y: int x: 1 Stack Space required for method m int numbers: int number: 1 reference Array of ten int values is stored here The arrays are stored in a heap. Heap reference Returning an Array from a Method public static int reverse(int list) { int result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; } int list1 = new int{1, 2, 3, 4, 5, 6}; int list2 = reverse(list1); list result Trace the reverse Method public static int reverse(int list) { int result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; } int list1 = new int{1, 2, 3, 4, 5, 6}; int list2 = reverse(list1); list result 1 2 3 4 5 6 Declare result and create array Trace the reverse Method, cont. Trace the reverse Method, cont....
View Full Document

This note was uploaded on 01/26/2012 for the course IT 108, 103, taught by Professor Bruno during the Spring '11 term at George Mason.

Page1 / 29

ChapterSixPartII - IT108 Arrays Part II Passing Arrays to...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online