การนำอัลกอริทึม String มาใช้
แนะนำใหม่
7:49
การเลือกและการทำซ้ำ
นี่คือลูปสามชุด สองชุดแรกแตกต่างกันเพียงหนึ่งตัวอักษร —เครื่องหมายน้อยกว่าแทนที่จะเป็นน้อยกว่าหรือเท่ากับ ลูปแรกทำงานสิบครั้ง ลูปที่สองทำงานสิบหนึ่ง…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English |
|---|
| StringIndexOutOfBoundsException/strɪŋ ˈɪndeks ˈaʊtəv baʊndz ekˈsepʃn/ |
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 ("aeiouAEIOU".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.
การวนลูปบนตัวอักษรของ HELLO
ลูปจะเข้าถึงดัชนี 0 ถึง length()-1
เงื่อนไขลูปที่ถูกต้องเพื่อเข้าถึงตัวอักษรทั้งหมดของ String s คือ...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ดัชนีที่มีvalidคือ 0..length()-1 ดังนั้น i < s.length()
การเข้าถึง index s.length() ของ String จะเกิด...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
indexสุดท้ายที่มีvalidคือ length()-1
การสร้าง String กลับด้าน เนื่องจาก String เป็น immutable คุณ...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
String เป็น immutable — สร้างอันใหม่ขึ้นมา
ลูปจะเข้าถึงตัวอักษรกี่ตัวสำหรับ String "HELLO" (โดยใช้ i < length())?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Indices 0..4 → 5 ตัวอักษร
รูปแบบตัวสะสมจากลูปยังใช้กับการนับตัวอักษรใน String ได้เช่นกัน
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
count++ ภายในลูป index จะนับตัวอักษรที่ตรงกัน
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้