lec13

lec13 - CSE 8A: Lecture 13 Returning arrays from methods...

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

View Full Document Right Arrow Icon
Page 1 of 20 CSE 8A UCSD LEC 13 CSE 8A: Lecture 13 Returning arrays from methods Comment translation as a coding strategy Command line arguments Partially filled arrays Parallel arrays Searching (Reading: Savitch, Ch. 6 and Ch 5)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Page 2 of 20 CSE 8A UCSD LEC 13 Midterm Exam #2 Midterm Exam #2 will be Thu Feb 28, during lecture time. Place: Lecture room Closed-book, closed-notes, no calculators. Bring picture ID! Coverage: Chapters 1-6, Lectures 1-14, Assignments P1-P6 A practice midterm is available online (PDF format) Lecture notes are available online Midterm review: Lecture Tues Nov 13
Background image of page 2
Page 3 of 20 CSE 8A UCSD LEC 13 Returning arrays from methods In Java, a method can return an array. As for other reference types, a pointer to the array is returned; the array is not copied As an example, here is a method that takes a String as argument, and returns the chars in the String as an array: public char[] stringToChars(String s) { // create the char array we will return char[] ch = new char[s.length()]; // set elements of the char array as required for(int i=0; i<ch.length; i++) { ch[i] = s.charAt(i); } // return a pointer to the char array return ch; } (There is actually an instance method of the String class that does this: public char[] toCharArray() )
Background image of page 3

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

View Full DocumentRight Arrow Icon
Page 4 of 20 CSE 8A UCSD LEC 13 Using arrays As an example of array use. .. Write a function that takes as argument an array of chars, and returns true if the array of chars is a palindrome; else false a palindrome is a string that is exactly the same forward or backward. .. so palindrome("Hello there!".toCharArray()) // false palindrome("ABLE WAS I ERE I SAW ELBA".toCharArray()) // true palindrome("AAAAABBBB".toCharArray()) // false palindrome("aaAbBb".toCharArray()) // false palindrome(stringToChars("1881")) // true
Background image of page 4
Page 5 of 20 CSE 8A UCSD LEC 13 Comment translation Let’s try the “comment translation” strategy for code development. .. In English pseudocode, write the algorithm the method will implement Put this English pseudocode in the body of the method with // ’s before each line, or enclosed in /* */ Translate the English pseudocode into Java The result: commented Java code that (hopefully) does what you want!
Background image of page 5

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

View Full DocumentRight Arrow Icon
CSE 8A UCSD LEC 13 A palindrome-testing method: first step, pseudocode Requirement: return true if the char array holds a palindrome;else return false Let n be the length of the char array For each i from 0 up to n. .. Compare the ith element from the front of the array to the ith element from the end. If they are different, return false immediately
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/12/2008 for the course CSE 8 taught by Professor Marx during the Fall '08 term at UCSD.

Page1 / 20

lec13 - CSE 8A: Lecture 13 Returning arrays from methods...

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

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