Strings and nested loops · Strings และ nested loops
Strings and nested loops
- A
Stringis a row of characters, each with a position (an index). - The first index is
0, and the last islength() - 1. - In this lesson you walk through a String one character at a time, and put loops inside loops.
Strings and nested loops
Stringคือแถวของตัวอักษร โดยแต่ละตัวมี ตำแหน่ง (ค่าดัชนี)- The first index is
0, and the last islength() - 1. - ในบทเรียนนี้ คุณจะวนผ่าน String ตัวละครละหนึ่งตัว และใส่ลูปภายในลูป
Traversing a String
s.length()gives the number of characters.s.charAt(i)gives the character at indexi.- Loop
ifrom0up tolength() - 1to visit every character.
public class Main {
public static void main(String[] args) {
String s = "code";
for (int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
}
}
Counting characters
- You can count how many times a character appears.
- Use a counter that starts at
0. - A single character uses single quotes, like
'o'. Compare with==(chars, not Strings).
Counting characters
- คุณสามารถนับได้ว่าตัวอักษรนั้นปรากฏกี่ครั้ง
- Use a counter that starts at
0. - ตัวอักษรเดียวใช้ เครื่องหมายอัญชี, เช่น
'o'เปรียบเทียบกับ==(chars ไม่ใช่ Strings)
public class Main {
public static void main(String[] args) {
String s = "balloon";
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'o') {
count++;
}
}
System.out.println(count); // 2
}
}
Building a new String
- Start with an empty String
""and add characters with+. - Reading from the end to the start reverses the String.
- Each
+makes a longer String.
Building a new String
- Start with an empty String
""and add characters with+. - การอ่านจาก ท้าย ไปหา ต้น จะทำให้กลับลำดับ String
- Each
+makes a longer String.
public class Main {
public static void main(String[] args) {
String s = "code";
String result = "";
for (int i = s.length() - 1; i >= 0; i--) {
result = result + s.charAt(i);
}
System.out.println(result); // edoc
}
}
Enhanced for (preview)
- For walking a String you usually need the index, so a normal
foris best. - But Java also has an enhanced for loop that visits items directly.
- You will use it a lot with arrays in lesson 8. Here is a small preview with text.
Enhanced for (preview)
- สำหรับการทำ Loop ผ่าน String มักต้องใช้ค่าดัชนี ดังนั้น
forแบบปกติจึงเหมาะที่สุด - แต่ Java ยังมีลูป enhanced for ที่สามารถเข้าถึงรายการโดยตรงได้
- คุณจะนำไปใช้บ่อยๆ กับ Array ในบทเรียน 8 นี่คือตัวอย่างเล็กๆ น้อยๆ ด้วยข้อความ
public class Main {
public static void main(String[] args) {
String[] words = {"hi", "there", "!"};
for (String word : words) {
System.out.println(word);
}
}
}
Nested loops
- A nested loop is a loop inside another loop.
- The inner loop runs all the way for each step of the outer loop.
- Below, the outer loop picks a row, and the inner loop prints stars in that row.
Nested loops
- Nested loop คือลูปที่อยู่ภายในอีกลูปหนึ่ง
- ลูปภายในจะทำงาน จนจบ สำหรับ ทุก ขั้นตอนของลูปภายนอก
- ด้านล่างนี้ ลูปภายนอกจะเลือกแถว และลูปภายในจะพิมพ์ดาวในแถวนั้น
public class Main {
public static void main(String[] args) {
for (int row = 1; row <= 3; row++) {
String line = "";
for (int col = 1; col <= row; col++) {
line = line + "*";
}
System.out.println(line);
}
}
}
Common mistakes
- Strings are immutable — store the result of
s.substring(...)etc. s.length()is a method (with()), unlike an array's.length.
ข้อผิดพลาดที่พบบ่อย
- Strings are immutable — store the result of
s.substring(...)etc. s.length()เป็น method (ซึ่งมี()) ต่างจาก.lengthของ Array
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
ลองดูเลย
- Each task เติม skeleton class ไว้ล่วงหน้า — เขียนโค้ดของคุณ ภายใน main, หรือเติมเต็ม method ที่แสดง
- กด Run เพื่อ compile และ run, แล้วกด Check answer
- โค้ดของคุณ compile และ run บน server, ดังนั้นแม้การ run ครั้งแรกก็เร็ว
Indexing a String
Each char has an index from 0; substring takes a range. · แต่ละ char มี index ตั้งแต่ 0; substring รับ range
Complete countChar(String s, char c) so it returns how many times c appears in s. Use charAt and ==. For example countChar("balloon", 'o') is 2. · เติมเต็ม countChar(String s, char c) เพื่อให้ return ว่า c ปรากฏใน s กี่ครั้ง. ใช้ charAt และ ==. ตัวอย่าง countChar("balloon", 'o') ได้ผลเป็น 2
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete reverse(String s) so it returns s with the letters in the opposite order. For example reverse("code") is "edoc". Build a new String with +. · เติมเต็ม reverse(String s) เพื่อให้ return s ที่มีตัวอักษรเรียงลำดับย้อนกลับ. ตัวอย่าง reverse("code") ได้ผลเป็น "edoc". สร้าง String ใหม่ด้วย +
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Use nested loops to print a triangle of stars: line 1 has one *, line 2 has two, line 3 has three, line 4 has four. Print each line with System.out.println. · ใช้ nested loops เพื่อพิมพ์รูปสามเหลี่ยมดาว: บรรทัดที่ 1 มี * หนึ่งดวง, บรรทัดที่ 2 มีสองดวง, บรรทัดที่ 3 มีสามดวง, บรรทัดที่ 4 มีสี่ดวง. พิมพ์แต่ละบรรทัดด้วย System.out.println
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่