ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Array ကို ကြိုတင် ဝယ်ထားတဲ့ ဥအိမ် ၁၂ ကွက်ပါ ဗူးတစ်ဗူးလို မြင်ကြည့်ပါ — ၁၃ လုံးမြောက်ကို ထည့်ချင်ရင် ဗူးအသစ် ဝယ်ပြီး ပြောင်းထည့်ရပါတယ်။ ArrayList ကတော့ ဆွဲဆန့်လို့ရတဲ့ အိတ်တစ်လုံးလိုပါပဲ၊ item တွေ ထည့်လေ ကြီးလာလေ ဖြစ်ပြီး ဖယ်လိုက်ရင်လည်း ကြားမှာ အပေါက် မကျန်ခဲ့ပါဘူး။
ArrayList က item တွေကို ထည့်ခဲ့တဲ့ အစဉ်အတိုင်း သိမ်းပြီး index 0 ကနေ စပါတယ်။ Todo list, shopping cart, search result စတဲ့ item အရေအတွက် ကြိုမသိနိုင်တဲ့ list တွေအတွက် Java developer တွေ အသုံးအများဆုံး collection ပါ။
Problem → Why → Solution
ပြဿနာ: array က size ပုံသေ ဖြစ်နေတာ
Task တွေကို String[] array နဲ့ သိမ်းထားရင် size ကို ဖန်တီးတဲ့အချိန်မှာ ဆုံးဖြတ်ရပါတယ်။ အောက်က code မှာ ကွက်လပ် ၂ ကွက်ပဲ ရှိလို့ တတိယ task ထည့်တာနဲ့ ArrayIndexOutOfBoundsException ဖြစ်ပါတယ်။ Task တစ်ခု ဖယ်ချင်ရင်လည်း နောက်က item တွေကို ကိုယ်တိုင် ရွှေ့ရပြီး ဘယ်နှခု တကယ်သုံးနေလဲဆိုတာကို count variable သီးသန့်နဲ့ မှတ်ထားရပါတယ်။
ArrayList ကို ပထမဆုံး သုံးတဲ့အခါမှာလည်း array အကျင့်ကြောင့် tasks.length လို့ ရေးမိတတ်ပါတယ် — ArrayList မှာ length field မရှိလို့ compile error တက်ပါတယ်။
public class Main {
public static void main(String[] args) {
String[] tasks = new String[2];
int count = 0;
tasks[count++] = "Learn Java syntax";
tasks[count++] = "Practice loops";
tasks[count++] = "Build mini project"; // ArrayIndexOutOfBoundsException: 2
System.out.println(count + " tasks");
}
}ဘာကြောင့် ArrayList လိုတာလဲ
Java array ရဲ့ length က ဖန်တီးပြီးတာနဲ့ ဘယ်တော့မှ မပြောင်းပါဘူး။ ArrayList က အတွင်းမှာ array တစ်ခုကို သုံးထားပေမယ့် နေရာမလောက်တော့ရင် ပိုကြီးတဲ့ array အသစ် ဖန်တီးပြီး item တွေကို ကူးပေးတဲ့ အလုပ်ကို အလိုအလျောက် လုပ်ပေးပါတယ်။ ကြားက item ဖယ်တဲ့အခါ နောက်က item တွေကို ရွှေ့ပေးတာလည်း သူပဲ လုပ်ပြီး size() က တကယ်ရှိတဲ့ item အရေအတွက်ကို အမြဲ မှန်ကန်စွာ ပြန်ပေးပါတယ်။
Core concept: အတွင်းမှာ ကြီးထွားနေတဲ့ array
ArrayList<String> ဆိုတာ "String တွေပဲ သိမ်းတဲ့ list" လို့ generic type နဲ့ ပြောတာပါ။ Generic type က reference type ဖြစ်ရလို့ ArrayList<int> လို့ ရေးလို့ မရပါဘူး — ArrayList<Integer> လို့ wrapper class ကို သုံးရပါတယ်။ list.add(5) လို့ ရေးရင် Java က int 5 ကို Integer object အဖြစ် အလိုအလျောက် ပြောင်းပေးတာကို autoboxing လို့ ခေါ်ပြီး ပြန်ထုတ်တဲ့အခါ unboxing ဖြစ်ပါတယ်။ Variable ကို List<String> tasks = new ArrayList<>(); လို့ interface type နဲ့ ကြေညာတာ အလေ့အထ ကောင်းပါတယ်၊ <> (diamond) က ညာဘက်မှာ type ကို ထပ်မရေးရအောင် လုပ်ပေးပါတယ်။
အတွင်းပိုင်းမှာ ArrayList က Object[] array တစ်ခုနဲ့ size counter တစ်ခုကို ကိုင်ထားပါတယ်။ Array ရဲ့ length ကို capacity လို့ ခေါ်ပြီး size() ကတော့ တကယ် ထည့်ထားတဲ့ item အရေအတွက်ပါ။ Capacity ပြည့်သွားရင် ပိုကြီးတဲ့ array အသစ်ကို ဖန်တီးပြီး item အားလုံး ကူးပါတယ် (OpenJDK မှာ ၁.၅ ဆလောက် ကြီးစေပေမယ့် ဒါက implementation detail ပါ)။ ဒီလို တစ်ခါတစ်ရံပဲ ကူးရလို့ နောက်ဆုံးမှာ add() လုပ်တာက ပျမ်းမျှအားဖြင့် မြန်ပါတယ်။
Array ပေါ်မှာ အခြေခံထားလို့ get(index) နဲ့ set(index, value) က item ဘယ်လောက်ပဲ များများ ချက်ချင်း ရပါတယ်။ ဒါပေမယ့် add(0, item) ဒါမှမဟုတ် remove(0) လို အစ ဒါမှမဟုတ် အလယ်မှာ ထည့်/ဖယ်ရင် နောက်က item အားလုံးကို တစ်နေရာစီ ရွှေ့ရလို့ list ကြီးလေ နှေးလေ ဖြစ်ပါတယ်။ contains() နဲ့ indexOf() ကလည်း equals() နဲ့ အစကနေ တစ်ခုချင်း ရှာရလို့ list ရှည်ရင် နှေးပါတယ်။ equals() နဲ့ တိုက်တာမို့ object မတူပေမယ့် စာသားတူတဲ့ String နှစ်ခုကို ကိုက်တယ်လို့ သတ်မှတ်ပြီး ကိုယ်ပိုင် class တွေမှာတော့ equals() ကို override လုပ်မှ ဒီ method တွေ မှန်မှန် အလုပ်လုပ်ပါတယ်။
Index က 0 ကနေ size() - 1 အထိပဲ မှန်ပါတယ်။ get(size()) လို အပြင်ဘက် index ကို ယူရင် IndexOutOfBoundsException ပစ်ပါတယ်။ ArrayList ရဲ့ toString() က [a, b, c] ပုံစံ ထုတ်ပေးလို့ debug လုပ်ရ လွယ်ပါတယ်။
- အတွင်းမှာ array + size counter
- add() က နောက်ဆုံး ကွက်လပ်ကို ဖြည့်
- ပြည့်ရင် ပိုကြီးတဲ့ array ကို ကူး
- အလယ်က ထည့်/ဖယ်ရင် item တွေ ရွှေ့
Index နဲ့ ယူတာ မြန်ပြီး အစ ဒါမှမဟုတ် အလယ်မှာ ပြောင်းတာ နှေးပါတယ်။
အထွေထွေ ပုံစံ
List<Type> list = new ArrayList<>();
list.add(item); // append at the end
list.add(index, item); // insert, shifting the rest right
Type value = list.get(index);
list.set(index, newItem); // replace
list.remove(index); // or list.remove(object)
boolean found = list.contains(item);
int count = list.size();Variable ကို List interface type နဲ့ ကြေညာပြီး ArrayList နဲ့ ဖန်တီးပါတယ်။ add() က နောက်ဆုံးမှာ ထည့်ပြီး add(index, item) က ကြားထဲ ထည့်ပါတယ်။ get/set က index နဲ့ ဖတ်/ပြောင်းပြီး remove() မှာ index version နဲ့ object version နှစ်မျိုး ရှိပါတယ်။ size() က item အရေအတွက်ပါ။
Example: task list ကို ပြင်ဆင်ခြင်း
ဒီ example က task list တစ်ခုကို နောက်ဆုံးမှာ ထည့်တာ၊ ကြားထဲ ထည့်တာ၊ value နဲ့ ဖယ်တာ၊ အစားထိုးတာနဲ့ ရှာတာဆိုတဲ့ ArrayList ရဲ့ နေ့စဉ်သုံး operation တွေနဲ့ တစ်ဆင့်ချင်း ပြင်ပြထားပါတယ်။ Line တစ်ကြောင်းစီ ပြီးတိုင်း list က ဘယ်လို ဖြစ်နေလဲဆိုတာ စာရွက်ပေါ်မှာ ချရေးကြည့်ပြီး output နဲ့ တိုက်ကြည့်ပါ။
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> tasks = new ArrayList<>();
tasks.add("Learn Java syntax");
tasks.add("Practice loops");
tasks.add("Build mini project");
tasks.add(1, "Read the docs"); // insert at index 1, shifts the rest right
tasks.remove("Practice loops"); // remove by value
tasks.set(0, "Review Java syntax"); // replace the item at index 0
System.out.println(tasks);
System.out.println("First task: " + tasks.get(0));
System.out.println("Total tasks: " + tasks.size());
System.out.println("Has docs task: " + tasks.contains("Read the docs"));
System.out.println("Index of project: " + tasks.indexOf("Build mini project"));
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 6
List<String> tasks = new ArrayList<>();String တွေပဲ လက်ခံတဲ့ list အလွတ်တစ်ခုကို ဖန်တီးပါတယ်။ Variable type က List ဖြစ်လို့ နောက်မှ implementation ပြောင်းချင်ရင် ညာဘက်ကိုပဲ ပြင်ရပါတယ်။
- Line 10
tasks.add(1, "Read the docs"); // insert at index 1, shifts the rest rightIndex 1 မှာ ထည့်လိုက်တော့ "Practice loops" နဲ့ "Build mini project" က တစ်နေရာစီ နောက်ကို ရွှေ့သွားပြီး list က [Learn Java syntax, Read the docs, Practice loops, Build mini project] ဖြစ်ပါတယ်။
- Line 12
tasks.remove("Practice loops"); // remove by valueString argument ပေးလို့ remove(Object) version ကို ခေါ်ပြီး equals() နဲ့ ကိုက်တဲ့ ပထမဆုံး item ကို ဖယ်ပါတယ်။ ဖယ်နိုင်ရင် true ပြန်ပါတယ်။
- Line 13
tasks.set(0, "Review Java syntax"); // replace the item at index 0set() က size မပြောင်းဘဲ index 0 က item ကို အစားထိုးပြီး item ဟောင်းကို return ပြန်ပါတယ်။
- Lines 15-19
System.out.println("Index of project: " + tasks.indexOf("Build mini project"));နောက်ဆုံး list က item ၃ ခု ကျန်ပါတယ်။ contains() က true ပြန်ပြီး indexOf() က 2 ပြန်ပါတယ်၊ မတွေ့ရင် -1 ပြန်ပါတယ်။
[Review Java syntax, Read the docs, Build mini project]
First task: Review Java syntax
Total tasks: 3
Has docs task: true
Index of project: 2remove(int) နဲ့ remove(Object) ထောင်ချောက်
List<Integer> မှာ remove() overload နှစ်ခုက ရှုပ်ထွေးစေပါတယ်။ ids.remove(1) လို့ ရေးရင် Java က int argument နဲ့ အတိအကျ ကိုက်တဲ့ remove(int index) ကို ရွေးလို့ value 1 ကို မဟုတ်ဘဲ index 1 က item ကို ဖယ်ပါတယ်။ Value ကို ဖယ်ချင်ရင် Integer.valueOf(1) နဲ့ object အဖြစ် ပြောင်းပေးရပါတယ်။
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> ids = new ArrayList<>(List.of(5, 1, 7, 1));
ids.remove(1); // remove(int index): removes the 1 at index 1
System.out.println(ids); // [5, 7, 1]
ids.remove(Integer.valueOf(1)); // remove(Object): removes the value 1
System.out.println(ids); // [5, 7]
int total = 0;
for (int id : ids) { // auto-unboxing Integer -> int
total += id;
}
System.out.println("Total: " + total);
}
}Loop ပတ်ရင်း ဘေးကင်းစွာ ဖယ်ခြင်း
for-each loop ပတ်နေရင်း list.remove() ကို ခေါ်ရင် ArrayList ရဲ့ iterator က list ပြောင်းသွားတာကို သိပြီး ConcurrentModificationException ပစ်ပါတယ်။ ဒီ check က "fail-fast" ဖြစ်ပေမယ့် best-effort သာ ဖြစ်လို့ တချို့ case တွေမှာ exception မပစ်ဘဲ item ကို ကျော်သွားတာမျိုးလည်း ဖြစ်နိုင်ပါတယ်။ Condition နဲ့ ဖယ်ချင်ရင် removeIf() ကို သုံးပါ၊ ဒါမှမဟုတ် Iterator ရဲ့ iterator.remove() ကို သုံးပါ။ List.of() နဲ့ ဖန်တီးတဲ့ list ကတော့ immutable ဖြစ်လို့ add/remove လုပ်ရင် UnsupportedOperationException ပစ်ပါတယ်။
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>(List.of("Aye", "", "Bo", "", "Chit"));
try {
for (String name : names) {
if (name.isEmpty()) names.remove(name); // modifies the list mid-loop
}
} catch (ConcurrentModificationException e) {
System.out.println("for-each + remove: ConcurrentModificationException");
}
names.removeIf(String::isEmpty); // safe bulk removal
System.out.println(names);
List<String> fixed = List.of("x", "y");
try {
fixed.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("List.of is immutable");
}
}
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Database query တစ်ခုရဲ့ ရလဒ် row တွေ၊ JSON API ကနေ ရလာတဲ့ item array တွေ၊ shopping cart ထဲက product တွေ၊ notification list တွေကို Java code မှာ List<T> အဖြစ် ကိုင်တွယ်ပြီး အများစုက ArrayList ပါ။ Spring Data JPA လို library တွေက repository method ကနေ List<Order> ပြန်ပေးပြီး Jackson လို JSON library က JSON array ကို ArrayList အဖြစ် ပြောင်းပေးပါတယ်။
Class တစ်ခုက သူ့အတွင်းက list ကို getter ကနေ တိုက်ရိုက် ပြန်ပေးရင် အပြင်က code က add/remove လုပ်ပြီး object ရဲ့ rule တွေကို ကျော်သွားနိုင်ပါတယ်။ ဒါကြောင့် return List.copyOf(items) ဒါမှမဟုတ် Collections.unmodifiableList(items) နဲ့ read-only view/copy ကိုပဲ ပေးပါတယ်။ Item အရေအတွက်ကို ကြိုသိရင် new ArrayList<>(expectedSize) နဲ့ capacity ကြိုပေးပြီး array ကူးတာကို လျှော့နိုင်ပါတယ်။ အစကနေ မကြာခဏ ထည့်/ဖယ်ရတဲ့ queue လိုအပ်ချက်မျိုးမှာတော့ ArrayDeque လို collection က ပိုသင့်တော်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. length ဒါမှမဟုတ် length() ကို သုံးတာ
Array မှာ tasks.length (field)၊ String မှာ name.length() (method) ဖြစ်ပေမယ့် collection တွေမှာ tasks.size() ပါ။ ArrayList ပေါ်မှာ length ကို ခေါ်ရင် "cannot find symbol" compile error တက်ပါတယ်။
2. List<Integer> မှာ remove(1) ကို value ဖယ်တယ်လို့ ထင်တာ
List<Integer> scores ပေါ်မှာ scores.remove(1) က index 1 က item ကို ဖယ်ပါတယ်။ List တိုရင် IndexOutOfBoundsException ဖြစ်ပြီး ရှည်ရင် မှားတဲ့ item ကို တိတ်တဆိတ် ဖယ်သွားပါတယ်။ Value ဖယ်ချင်ရင် scores.remove(Integer.valueOf(1)) လို့ ရေးပါ။
3. for-each ထဲမှာ add/remove လုပ်တာ
for-each loop ထဲမှာ list ကို ပြောင်းရင် ConcurrentModificationException ဖြစ်တတ်ပါတယ်။ removeIf(), Iterator.remove() ဒါမှမဟုတ် list အသစ်တစ်ခုထဲ စုတာကို သုံးပါ။
4. List.of() ရလဒ်ကို ပြင်ဖို့ ကြိုးစားတာ
List.of("a", "b") က immutable list ဖြစ်လို့ add() က UnsupportedOperationException ပစ်ပါတယ်။ ပြင်ချင်ရင် new ArrayList<>(List.of(...)) နဲ့ ကူးယူပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Variable, parameter နဲ့ return type တွေကို ArrayList အစား List interface နဲ့ ကြေညာပါ။
- ပြောင်းစရာ မလိုတဲ့ ပုံသေ data အတွက် List.of() ကို သုံးပြီး ပြင်ရမယ့် list အတွက်ပဲ ArrayList ကို သုံးပါ။
- List<Integer> ပေါ်က value ဖယ်တဲ့ code မှာ Integer.valueOf() ကို ပြတ်ပြတ်သားသား ရေးပြီး ဖတ်တဲ့သူ မရှုပ်အောင် comment ထည့်ပါ။
Mini challenge
Mini challenge: စာမေးပွဲ အမှတ်စာရင်း
အမှတ်စာရင်း ArrayList ထဲက ရိုက်မှားထားတဲ့ value 1 ကို ဖယ်ပြီး အောင်မှတ် (50) နဲ့ အထက်ပဲ ပါတဲ့ read-only list ကို ပြန်ပေးတဲ့ method နဲ့ ပျမ်းမျှ တွက်တဲ့ method ရေးပါ။ မူရင်း list ကို မပြောင်းရပါဘူး။
- Index 1 မဟုတ်ဘဲ value 1 ကို ဖယ်ရမယ်
- passingScores() က input list ကို မထိဘဲ copy ပေါ်မှာ removeIf သုံးရမယ်
- passingScores() က List.copyOf() နဲ့ read-only list ပြန်ပေးရမယ်
- average() က list အလွတ်ဆိုရင် 0 ပြန်ပြီး ဒသမ တစ်နေရာနဲ့ print ထုတ်ရမယ်
- Read-only list ကို add() လုပ်ကြည့်ပြီး exception ကို ဖမ်းပြရမယ်
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class Main {
static List<Integer> passingScores(List<Integer> scores, int passMark) {
// TODO: copy the input, removeIf below passMark, return a read-only copy
return null;
}
static double average(List<Integer> scores) {
// TODO: return 0 for an empty list, otherwise total / size as a double
return 0;
}
public static void main(String[] args) {
List<Integer> scores = new ArrayList<>(List.of(72, 45, 90, 1, 58));
// TODO: remove the VALUE 1, print scores, passing scores, and the average
// TODO: show that the passing list cannot be modified
}
}ရလဒ်: Program က ဒီ output ကို ထုတ်ရပါမယ်: Scores: [72, 45, 90, 58] Passing: [72, 90, 58] Original untouched: 4 scores Average: 73.3 Passing list is read-only
အရိပ်အမြွက် ကြည့်ရန်
remove(Integer.valueOf(1)) က value ကို ဖယ်ပါတယ်။ Integer division ကို ရှောင်ဖို့ (double) total / scores.size() လို့ cast လုပ်ပါ။ Locale အလိုက် ဒသမ အမှတ်အသား မပြောင်းအောင် String.format(Locale.ROOT, "%.1f", value) ကို သုံးပါ။
အဖြေ ကြည့်ရန်
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class Main {
static List<Integer> passingScores(List<Integer> scores, int passMark) {
List<Integer> result = new ArrayList<>(scores);
result.removeIf(score -> score < passMark);
return List.copyOf(result);
}
static double average(List<Integer> scores) {
if (scores.isEmpty()) {
return 0;
}
int total = 0;
for (int score : scores) {
total += score;
}
return (double) total / scores.size();
}
public static void main(String[] args) {
List<Integer> scores = new ArrayList<>(List.of(72, 45, 90, 1, 58));
scores.remove(Integer.valueOf(1)); // the value 1 was a typo, not index 1
System.out.println("Scores: " + scores);
List<Integer> passing = passingScores(scores, 50);
System.out.println("Passing: " + passing);
System.out.println("Original untouched: " + scores.size() + " scores");
String avg = String.format(Locale.ROOT, "%.1f", average(passing));
System.out.println("Average: " + avg);
try {
passing.add(100);
} catch (UnsupportedOperationException e) {
System.out.println("Passing list is read-only");
}
}
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- ArrayList က အတွင်းမှာ array ကို သုံးပြီး ပြည့်ရင် ပိုကြီးတဲ့ array ကို ကူးပေးလို့ size ပုံသေ မဟုတ်ပါဘူး။
- get/set က မြန်ပြီး အစ ဒါမှမဟုတ် အလယ်မှာ ထည့်/ဖယ်ရင် item တွေ ရွှေ့ရလို့ နှေးပါတယ်။
- Item အရေအတွက်က size() ဖြစ်ပြီး generic type မှာ int အစား Integer လိုပါတယ်၊ List<Integer> မှာ remove(int) နဲ့ remove(Object) ကို သတိထားပါ။
- Loop ပတ်ရင်း ဖယ်ဖို့ removeIf() ကို သုံးပြီး အပြင်ကို list ပေးရင် List.copyOf() နဲ့ read-only copy ပေးပါ။
Java SE 25 API Specification — Oracle