เมื่อทำงานกับอาร์เรย์ใน Java คุณอาจต้องการตรวจสอบว่าอาร์เรย์มีค่าที่ต้องการหรือไม่
เช่น สร้างโปรแกรมติดตามความคืบหน้าการสังเกตสัตว์นกฤดูกาลนี้ อาจต้องการฟังก์ชันตรวจสอบว่าเคยเห็นสปีชีส์นกตัวนั้นมาหรือยัง
มีหลายวิธีตรวจสอบว่าอาร์เรย์มีค่าที่ต้องการ บทช่วยสอนนี้จะอธิบาย 3 วิธี: for-each loop, contains(), และ stream() พร้อมตัวอย่าง
อาร์เรย์ใน Java
ใน Java อาร์เรย์เป็นคอนเทนเนอร์จัดเก็บข้อมูลชนิดเดียว เช่น อาร์เรย์เก็บชื่อ 14 สปีชีส์นกที่เห็นในปีนี้ จะมี 14 ค่า string
อาร์เรย์สำคัญ เพราะให้นักพัฒนาจัดเก็บค่ามากมายในตัวแปรเดียว ได้ ประหยัดกว่าเก็บแยกตัวแปร และช่วยรักษาความอ่านง่ายของโปรแกรม
ตัวอย่างอาร์เรย์ใน Java:
String[] bagelShops; bagelShops = new String[4];
ประกาศอาร์เรย์ bagelShops จัดเก็บ String แล้วกำหนดขนาด 4 ค่า โค้ดกำหนด 4 ร้านเบเกิล:
String[] bagelShops = {"Absolute Bagels", "Bo's Bagels", "Brooklyn Bagel and Coffee Company", "Forest Hills Bagel"};
แต่จะตรวจสอบว่าอาร์เรย์มีค่าที่ต้องการยังไง? เช่น ต้องการเช็คว่า "Forest Hills Bagel" อยู่ในรายการโปรดไหม มาดูวิธีต่างๆ
ใช้ for-each loop
for-each loops (enhanced for loops) เป็นรูปแบบ for loop วนผ่านทุกสมาชิกในอาร์เรย์หรือคอลเลกชัน เช่น วนเหรียญในอาร์เรย์เก็บเหรียญคอลเลกชัน
เพื่อเช็คว่าอาร์เรย์มีค่าที่ต้องการ ใช้ for loop วนทุกสมาชิก เช็คว่าแต่ละตัวเท่ากับค่าที่หรือไม่
สมมติมีรายการร้านเบเกิลโปรดใน NYC ต้องการเช็คว่า "Absolute Bagels" อยู่ในลิสต์ไหม:
class Main {
public static void main(String[] args) {
String[] bagelShops = {"Absolute Bagels", "Bo's Bagels", "Brooklyn Bagel and Coffee Company", "Forest Hills Bagel"};
String shopToFind = "Absolute Bagels";
boolean found = false;
for (String shop : bagelShops) {
if (shop == shopToFind) {
found = true;
break;
}
}
if (found) {
System.out.println("Absolute Bagels is on your list of favorite bagel stores!");
} else {
System.out.println("Absolute Bagels is not on your list of favorite bagel stores.");
}
}
}
ผลลัพธ์: Absolute Bagels is on your list of favorite bagel stores.
วิเคราะห์: ประกาศอาร์เรย์ bagelShops 4 ร้าน ตัวแปร shopToFind = "Absolute Bagels" ค่าที่ต้องการหา ตัวแปร found ติดตามว่าพบหรือยัง
for-each loop วนทุกสมาชิกใน bagelShops 用 if เช็คว่าแต่ละร้านเท่ากับ shopToFind ไหม
ถ้า true: found = true, break หยุดลูป ถ้า false: ไม่ทำใน if
ท้ายโปรแกรม: ถ้า found = true พิมพ์ว่าอยู่ในลิสต์ ไม่เช่นนั้น พิมพ์ว่าไม่อยู่
วิธีนี้ใช้กับตัวเลขได้เช่นกัน ถ้ามีลิสต์ตัวเลข ต้องการเช็คว่ามีตัวเลขเฉพาะอยู่ไหม ใช้โค้ดคล้ายกัน แค่เปลี่ยนเป็นตัวเลข
ใช้เมธอด contains()
ตรวจสอบว่าอาร์เรย์มีค่าที่ต้องการได้ด้วยเมธอด contains() ของ Java รับพารามิเตอร์ 1 ตัว: ค่าที่ต้องการค้นหาในอาร์เรย์
ข้อจำกัด: contains() ใช้ได้เฉพาะกับอาร์เรย์ String เท่านั้น หากค้นหาตัวเลข ควรใช้ for-each loop แทน
ตัวอย่าง: มีรายการวงดนตรีโปรด ต้องการเช็คว่า "Beatles" อยู่ในลิสต์ไหม:
class Main {
public static void main(String[] args) {
String[] favoriteBands = {"Beatles", "Revivalists", "Jonas Brothers"};
String favoriteToFind = "Beatles";
boolean found = favoriteBands.contains(favoriteToFind);
if (found) {
System.out.println("Beatles is on your list of favorite bands!");
} else {
System.out.println("Beatles is not on your list of favorite bands.");
}
}
}
ผลลัพธ์: Beatles is on your list of favorite bands!
วิเคราะห์: ประกาศอาร์เรย์ favoriteBands ตัวแปร favoriteToFind เก็บชื่อวงที่หา ใช้ contains() เช็คว่า favoriteBands มีค่าใน favoriteToFind ("Beatles") ผลลัพธ์กำหนดให้ boolean found
ถ้า found = true พิมพ์ว่าอยู่ในลิสต์ ไม่อยู่ก็พิมพ์ว่าไม่อยู่
ใช้เมธอด stream()
แพ็กเกจ Java Arrays มีฟังก์ชันทำงานกับอาร์เรย์ หนึ่งในนั้นคือ stream() ใช้เช็คว่าอาร์เรย์มีค่าที่ต้องการได้ ต้องใช้ร่วมกับ anyMatch()
ตัวอย่าง: มีรายการซีรีส์โปรดของเพื่อน Ada ต้องการหาว่า "Parks and Recreation" อยู่ในลิสต์ไหม:
import java.util.Arrays;
class Main {
public static void main(String[] args) {
String[] favoriteShows = {"Billions", "Silicon Valley", "Superstore"};
String showToFind = "Parks and Recreation";
boolean found = Arrays.stream(favoriteShows).anyMatch(show -> show.equals(showToFind));
if (found) {
System.out.println("Parks and Recreation is on Ada's list of favorite TV shows.");
} else {
System.out.println("Parks and Recreation is not on Ada's list of favorite TV shows.");
}
}
}
ผลลัพธ์: Parks and Recreation is not on Ada's list of favorite TV shows.
วิเคราะห์: import Java Arrays ก่อน ประกาศอาร์เรย์ favoriteShows ตัวแปร showToFind = "Parks and Recreation" ใช้ Arrays.stream() และ anyMatch() เช็คว่า showToFind มีใน favoriteShows ไหม คืนค่า true/false ให้ found
ถ้า found = true พิมพ์ว่าอยู่ในลิสต์ ไม่อยู่ก็พิมพ์ว่าไม่อยู่
สรุป
การตรวจสอบว่าอาร์เรย์มีค่าที่ต้องการ เป็นการทำงานทั่วไปใน Java 3 วิธีหลัก:
- for-each loop
- contains()
- stream()
บทช่วยสอนนี้อธิบายพร้อมตัวอย่าง วิธีเช็คว่าอาร์เรย์ใน Java มีค่าที่ต้องการ ตอนนี้คุณมีทักษะเช็ค Java array ว่ามีค่าเฉพาะอยู่ไหม อย่างมืออาชีพแล้ว!