lecture14-arrays3

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

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

View Full Document Right Arrow Icon
cture 4: rrays rap- p) Lecture 14: Arrays (wrap up) CS 170, Section 000 0 ctober 2009 20 October 2009 10/20/2009 CS170, Section 000, Fall 2009 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
6.3: Copying Arrays (review) Problem: duplicate an array or a part of an ray nt list1 = new int[10]; array int list1 = new int[10]; int list2 = new int[10]; //copy list1 to list2 list2 = list1; 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Copying Arrays Using a loop: int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; or (int i = 0; i < sourceArrays.length; i++) for (int i 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i]; 4
Background image of page 4
The arraycopy method System.arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); Example: System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Review question In the following code, what is the printout for t2? list2? class Test { public static void main(String[] args) { nt ] 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
Background image of page 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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 ethod body will affect the original array that method body will affect the original array that was passed as the argument. 8
Background image of page 8
Simple Example public class Test { ublic static void main(String[] rgs { 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]); } ublic static void m( nt umber nt ] 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 } }
Background image of page 9

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

View Full DocumentRight Arrow Icon
Call Stack When invoking m(x, y), the values of x and y e passed to number and numbers 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 alues is 10 values is
Background image of page 10
Call Stack Stack Space required for method m Heap Space required for the int[] numbers: int number: 1 5555 0 The arrays are stored in a heap. reference main method int[] y: int x: 1 reference 0 Array of ten int values is stored here 11
Background image of page 11

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

View Full DocumentRight Arrow Icon
Returning an Array from a Method: Problem: reverse array public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0; i < list.length; i++) { result[result.length – i - 1] = list[i]; } return result; public static void main(String[] args) { } int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); } 12
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/03/2010 for the course CS CS 170 taught by Professor Eugeneagichtein during the Fall '09 term at Emory.

Page1 / 57

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

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

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