Triển khai Các thuật toán String
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| StringIndexOutOfBoundsException/strɪŋ ˈɪndeks ˈaʊtəv baʊndz ekˈsepʃn/ | StringIndexOutOfBoundsException |
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.
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==.

Reversing a string by walking its indexes backwards
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.
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.
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.
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".
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.
Lặp qua các ký tự của HELLO
A loop truy cập các chỉ số từ 0 đến length()-1.
Điều kiện vòng lặp đúng để truy cập mọi ký tự của String s là...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Các chỉ số hợp lệ là 0..length()-1, vì vậy i < s.length().
Truy cập chỉ số s.length() của một String sẽ gây ra...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chỉ số hợp lệ cuối cùng là length()-1.
Để xây dựng một String đảo ngược, do Strings bất biến, bạn...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Strings là bất biến — hãy xây dựng một cái mới.
Một vòng lặp truy cập bao nhiêu ký tự cho String "HELLO" (dùng i < length())?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Các chỉ số 0..4 → 5 ký tự.
Mẫu tích lũy từ vòng lặp cũng áp dụng để đếm ký tự trong một String.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
count++ bên trong một vòng lặp chỉ số đếm các ký tự khớp.