24a-section-4-solutions

Charati ch result strcharati return result a

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: i = 0; i < str.length(); i++) { if (str.charAt(i) != ch) { result += str.charAt(i); } } return result; } A slightly different approach that involves a while loop instead of a for loop: private String removeAllOccurrences(String str, char ch) { while (true) { int pos = str.indexOf(ch); if (pos >= 0) { str = str.substring(0, pos) + str.substring(pos + 1); } else break; } return str; } –2– 3. Heap/Stack diagrams heap 1000 num den 1 2 1020 num den 1 1 1040 num den garbage 1 2 i 1060 num den stack 1 4 result n x 3 1080 3 1000 1080 num den 1 8 4. Tracing method execution FFB0 FFAC FFB4 FFB0 FFB8 FFB4 FFB8 FFB8 FFBC r 1000 FFC0 FFC0...
View Full Document

This note was uploaded on 02/06/2014 for the course CS 106X taught by Professor Cain,g during the Winter '08 term at Stanford.

Ask a homework question - tutors are online