lecture15-arrays+String

lecture15-arrays+String - Lecture 15: Arrays String Lecture...

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

View Full Document Right Arrow Icon
cture 5: rrays String Lecture 15: Arrays + String CS 170, Section 000 2 ctober 2009 22 October 2009 10/22/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: explain different solutions to Lab 7 Command-line arguments Optional: more examples of 2D arrays hapter 8: String class ¾ Chapter 8: String class 10/22/2009 CS170, Section 000, Fall 2009 2
Background image of page 2
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); } 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
Trace the reverse Method int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); Declare result and create array public static int[] reverse(int[] list) { int[] result = new int[list.length]; or (int i = 0; i < list length; i++) { for (int i = 0; i < list.length; i++) { result[result.length – i - 1] = list[i]; } eturn result; return result; } list 1 23 4 5 6 4 result 0 00 0 0 0
Background image of page 4
Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 0 public static int[] reverse(int[] list) { int[] result = new int[list.length]; or (int i = 0; i < list length; i++) { for (int i = 0; i < list.length; i++) { result[result.length – i - 1] = list[i]; } eturn result; return result; } list 1 23 4 5 6 5 result 0 00 0 0 0
Background image of page 5

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

View Full DocumentRight Arrow Icon
Trace the reverse Method, cont. i = 0 int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); public static int[] reverse(int[] list) { int[] result = new int[list.length]; Assign list[0] to result[5] for (int i = 0; i < list.length; i++) { result[result.length – i - 1] = list[i]; } return result; } list 1 23 4 5 6 6 result 0 00 0 0 1
Background image of page 6
Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); fter this i becomes 1 public static int[] reverse(int[] list) { int[] result = new int[list.length]; After this, i becomes 1 for (int i = 0; i < list.length; i++) { result[result.length – i - 1] = list[i]; } return result; } list 1 23 4 5 6 7 result 0 00 0 0 1
Background image of page 7

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

View Full DocumentRight Arrow Icon
Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); (=1) is less than 6 public static int[] reverse(int[] list) { int[] result = new int[list.length]; i for (int i = 0; i < list.length; i++) { result[result.length – i - 1] = list[i]; } return result; } list 1 23 4 5 6 8 result 0 00 0 0 1
Background image of page 8
Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 1 public static int[] reverse(int[] list) { int[] result = new int[list.length]; Assign list[1] to result[4] for (int i = 0; i < list.length; i++) { result[result.length – i - 1] = list[i]; } return result; } list 1 23 4 5 6 9 result 0 00 0 2 1
Background image of page 9

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

View Full DocumentRight Arrow Icon
Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 2 public static int[] reverse(int[] list) { int[] result = new int[list.length]; Assign list[2] to result[3] for (int i = 0; i < list.length; i++) { result[result.length – i - 1] = list[i]; } return result; } list 1 23 4 5 6 10 result 0 00 3 2 1
Background image of page 10
Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 3 public static int[] reverse(int[] list) { int[] result = new int[list.length]; Assign list[3] to result[2] for (int i = 0; i < list.length; i++) { result[result.length – i - 1] = list[i]; } return result; } list 1 23 4 5 6 11 result 0 04 3 2 1
Background image of page 11

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

View Full DocumentRight Arrow Icon
Trace the reverse Method, cont.
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 / 56

lecture15-arrays+String - Lecture 15: Arrays String Lecture...

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