{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture15-arrays+String

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

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

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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
Trace the reverse Method, cont.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online