Implementing String Algorithms · 实现字符串算法
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| StringIndexOutOfBoundsException/strɪŋ ˈɪndeks ˈaʊtəv baʊndz ekˈsepʃn/ | 越界异常 | yuè jiè yì cháng |
Looping over characters
- To process a String, loop over its indices from
0tolength() - 1. for (int i = 0; i < s.length(); i++) { ... s.substring(i, i+1) ... }s.substring(i, i+1)gives the one-character String at indexi.- This visits every character in order — the basis of most String algorithms.
遍历字符
- 要处理一个 String,从
0到length() - 1遍历它的下标。 for (int i = 0; i < s.length(); i++) { ... s.substring(i, i+1) ... }s.substring(i, i+1)给出下标i处的单字符 String。- 这按顺序访问每个字符——大多数字符串算法的基础。
Counting characters
- Combine the loop with an
ifto count matching characters. - "Count the vowels": loop, and
ifthe current character is a vowel,count++. - The accumulator pattern from 2.9 applies directly to Strings.
- Compare characters with
.equals(), not==.
数字符
- 把循环与一个
if组合起来数匹配的字符。 - “数元音”:循环,如果当前字符是元音就
count++。 - 2.9 的累加器模式直接适用于字符串。
- 用
.equals()比较字符,而非==。
Building a new String
- To transform text, build up a new String with
+, since Strings are immutable. String result = "";thenresult = result + something;inside the loop.- "Reverse a String": append each character from the end toward the start.
- The original never changes — you accumulate a fresh String.
构建一个新 String
- 要变换文本,用
+构建一个新 String,因为 String 是不可变的。 String result = "";然后在循环里result = result + something;。- “反转一个 String”:从末尾向开头逐个追加字符。
- 原来的从不改变——你累加出一个全新的 String。
Index bounds
- Valid indices run
0tolength() - 1; going outside throws a StringIndexOutOfBoundsException 越界异常. s.substring(i, i+1)is safe only whilei < s.length().- The loop condition
i < s.length()keepsiin range. - Off-by-one bounds are the most common String-loop bug.
下标边界
- 有效下标从
0到length() - 1;越出会抛出 StringIndexOutOfBoundsException(越界异常)。 s.substring(i, i+1)只有在i < s.length()时才安全。- 循环条件
i < s.length()让i保持在范围内。 - 差一的边界是最常见的字符串循环错误。
Loop String indices with i < s.length(), not i <= s.length(). The last valid index is length() - 1; touching index length() throws a StringIndexOutOfBoundsException. And build transformed text by accumulating a new String (result += ch) — you can't edit a String in place, because Strings are immutable.
用 i < s.length() 遍历字符串下标,而非 i <= s.length()。最后一个有效下标是 length() - 1;碰下标 length() 会抛出 StringIndexOutOfBoundsException。并通过累加一个新 String(result += ch)来构建变换后的文本——你无法就地编辑一个 String,因为 String 是不可变的。
Counting vowels in s:
int count = 0;for (int i = 0; i < s.length(); i++) { String ch = s.substring(i, i+1); if ("aeiou".indexOf(ch) >= 0) count++; }- Visits each character; counts it if it appears in
"aeiou".
数 s 中的元音:
int count = 0;for (int i = 0; i < s.length(); i++) { String ch = s.substring(i, i+1); if ("aeiou".indexOf(ch) >= 0) count++; }- 访问每个字符;如果它出现在
"aeiou"里就数它。
Process a String by looping its indices 0 to length()-1 (condition i < s.length()), reading each character with s.substring(i, i+1). Count with an if, or build a new String by accumulating with + (Strings are immutable). Staying in bounds avoids a StringIndexOutOfBoundsException.
处理一个 String 靠遍历它的下标 0 到 length()-1(条件 i < s.length()),用 s.substring(i, i+1) 读每个字符。用一个 if 计数,或用 + 累加构建一个新 String(String 不可变)。保持在范围内可避免 StringIndexOutOfBoundsException。
Looping over the characters of HELLO · 遍历 HELLO 的字符
A loop visits indices 0 to length()-1. · 循环访问下标 0 到 length()-1。
The correct loop condition to visit every character of String s is... · 访问 String s 每个字符的正确循环条件是……
Valid indices are 0..length()-1, so i < s.length(). · 有效下标是 0..length()-1,所以 i < s.length()。
Accessing index s.length() of a String throws... · 访问一个 String 的下标 s.length() 会抛出……
The last valid index is length()-1. · 最后一个有效下标是 length()-1。
To build a reversed String, since Strings are immutable, you... · 要构建一个反转的 String,由于 String 不可变,你……
Strings are immutable — build a fresh one. · String 不可变——构建一个新的。
How many characters does a loop visit for the String "HELLO" (using i < length())? · 对 String "HELLO"(用 i < length()),循环访问多少个字符?
Indices 0..4 → 5 characters. · 下标 0..4 → 5 个字符。
The accumulator pattern from loops also applies to counting characters in a String. · 循环里的累加器模式也适用于数字符串中的字符。
count++ inside an index loop counts matching characters. · 在下标循环里的 count++ 数匹配的字符。