{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture14-arrays3

# lecture14-arrays3 - Lecture 14 Arrays(wrap Lecture 14...

This preview shows pages 1–14. Sign up to view the full content.

Lecture 14: Arrays (wrap-up) Lecture 14: Arrays (wrap up) CS 170, Section 000 20 October 2009 10/20/2009 CS170, Section 000, Fall 2009 1

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

View Full Document
Lecture Plan Review: Array copying (Ch. 6.3) ¾ Passing arrays to/from methods (Ch. 6.4-5) ¾ Searching arrays (Ch. 6.7), arrays class (6.8) ¾ Two-dimensional arrays (Ch. 6.10) ¾ Last 5 minutes: Homework 4 overview (due Monday, 26 th October) 10/20/2009 CS170, Section 000, Fall 2009 2
6.3: Copying Arrays (review) Problem: duplicate an array or a part of an array int list1 = new int[10]; int list2 = new int[10]; //copy list1 to list2 list2 = list1; 3

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

View Full Document
Copying Arrays Using a loop: int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; for (int i = 0; i < sourceArrays.length; i++) for (int i 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i]; 4
The arraycopy method System.arraycopy(sourceArray, src_pos, targetArray, l h) tar_pos, length); Example: System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); 5

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

View Full Document
Review question In the following code, what is the printout for list2? class Test { public static void main(String[] args) { int[] list1 = {1 2 3}; int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + " "); } } 6
6.4: Passing Arrays to Methods public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } public static void main(String[] array) { int[] list = {3, 1, 2, 6, 4, 2}; printArray(list); } 7

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

View Full Document
Pass By Value What was pass by value? For parameter of an array type, the value of parameter contains a reference to an array Any changes to the array that occur inside the method body will affect the original array that method body will affect the original array that was passed as the argument. 8
Simple Example 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) { 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] } 9 }

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

View Full Document
Call Stack When invoking m(x, y), the values of x and y are passed to number and numbers y contains the reference value to the array! Stack Space required for method m Heap Space required for the int[] numbers: int number: 1 0 0 The arrays are stored in a heap. reference main method int[] y: int x: 1 reference 0 Array of ten int values is 10 values is