ArrayList (ကြီးလာ၊ သေးသွားနိုင်တဲ့ list)

B2.2.2: Construct programs that apply ArrayLists: add, remove, and traverse elements in a dynamic list.
မြန်မာလို: Dynamic list (ကြီးလာ၊ သေးသွားနိုင်တဲ့ list) တစ်ခုထဲမှာ element (အစုထဲက အချက်) တွေကို ထည့်ဖို့ (add)၊ ဖယ်ထုတ်ဖို့ (remove) နဲ့ traverse (element တိုင်းကို တစ်ခုချင်း ဖြတ်သွားခြင်း) လုပ်ဖို့ ArrayList တွေကို အသုံးချတဲ့ program တွေကို ကိုယ်တိုင် တည်ဆောက်နိုင်ရပါမယ်။


အဓိက အယူအဆများ

ArrayList ကို ဘာကြောင့် သုံးသလဲ

Array (အရွယ်အစား ပုံသေ၊ အမျိုးအစားတူ ဒေတာစု) တွေရဲ့ အရွယ်အစားက ပုံသေပါ: ဖန်တီးပြီးရင် ကြီးအောင်လည်း မလုပ်နိုင်၊ သေးအောင်လည်း မလုပ်နိုင်ပါဘူး။ Array ကို ဖန်တီးတဲ့အချိန်မှာ element ဘယ်နှစ်ခု လိုမလဲ မသိသေးတဲ့အခါ၊ ဒါမှမဟုတ် program run နေစဉ် အရေအတွက် ပြောင်းလဲနေတဲ့အခါ ဒါက ပြဿနာ ဖြစ်ပါတယ်။

String[] names = new String[5];  // exactly 5 slots: what if we need 6?
names = []   # a Python list starts empty and grows as needed - no size to choose

ArrayList က အလိုအလျောက် ကြီးလာ၊ သေးသွားတဲ့ dynamic list တစ်ခုပါ။ Capacity (သိမ်းနိုင်တဲ့ နေရာ အရေအတွက်) ကို စိတ်ပူစရာ မလိုဘဲ element တွေကို ထည့်နိုင်ပြီး element တွေကို ဖယ်ထုတ်တဲ့အခါလည်း ကွက်လပ် မကျန်ခဲ့ပါဘူး။

import java.util.ArrayList;

ArrayList<String> names = new ArrayList<>();  // starts empty, grows as needed
names.add("Alice");   // size is now 1
names.add("Bob");     // size is now 2
names.remove(0);      // size is now 1
# Java's ArrayList maps to the plain Python list - no import needed
names = []              # starts empty, grows as needed
names.append("Alice")   # size is now 1
names.append("Bob")     # size is now 2
names.pop(0)            # size is now 1

Array ကို ဘယ်အချိန် သုံးမလဲ: ဖန်တီးတဲ့အချိန်မှာ အရွယ်အစား အတိအကျကို သိတဲ့အခါ (ဥပမာ တစ်ပတ်ရဲ့ နေ့တွေ၊ အရွယ်အစား ပုံသေ ဇယားကွက်)။ ArrayList ကို ဘယ်အချိန် သုံးမလဲ: Program run နေစဉ် (runtime) အရွယ်အစား ပြောင်းလဲတဲ့အခါ (ဥပမာ အတန်းရဲ့ ကျောင်းသား စာရင်း၊ online ဈေးဝယ်ခြင်းထဲက ပစ္စည်းစာရင်း၊ ရှာဖွေမှု ရလဒ်များ)။

ဒီပုံစံကို သိပြီးသားပါ

Scanner နဲ့ String ကို သုံးတုန်းက import၊ new နဲ့ .method() တွေကို သုံးဖူးပြီးသားပါ။ ArrayList ကလည်း အဲဒီ ပုံစံအတိုင်းပါပဲ:

// Scanner (you already know this):
import java.util.Scanner;                    // import from java.util
Scanner input = new Scanner(System.in);      // create with new
input.nextLine();                            // call methods with dot notation

// ArrayList (same pattern):
import java.util.ArrayList;                  // import from java.util
ArrayList<String> names = new ArrayList<>();  // create with new
names.add("Alice");                          // call methods with dot notation
# Python's built-ins need no import and no new keyword:
text = input()          # read a line of input (like Scanner's nextLine)

names = []              # create an empty list (like new ArrayList<>())
names.append("Alice")   # call methods with dot notation - same idea as Java

<String> အပိုင်းကို generics (သိမ်းမယ့် ဒေတာ type ကို ထောင့်ကွင်း < > ထဲမှာ သတ်မှတ်ပေးတဲ့ ပုံစံ) လို့ ခေါ်ပါတယ်။ ဒီအပိုင်းက list ထဲမှာ ဘယ် ဒေတာ အမျိုးအစားကို သိမ်းမလဲဆိုတာ Java ကို ပြောပြပါတယ်။ အကြောင်းရင်း အပြည့်အစုံကို OOP (object တွေကို အခြေခံပြီး program ရေးနည်း) ကို လေ့လာတဲ့အခါ နားလည်လာပါလိမ့်မယ်။ အခုအတွက်တော့ public static void main လိုပဲ “ဒီအတိုင်း သုံးလိုက်ပါ” ဆိုတဲ့ စည်းမျဉ်းမျိုးပါ။

Static Data Structure နဲ့ Dynamic Data Structure (B2.2.1)

Array က static data structure (အရွယ်အစား ပုံသေ data structure) ဖြစ်ပြီး ArrayList က dynamic data structure (ကြီးလာ၊ သေးသွားနိုင်တဲ့ data structure) ဖြစ်ပါတယ်။ (Data structure ဆိုတာ ဒေတာ သိမ်းဆည်းပုံကို ဆိုလိုပါတယ်။) IB Computer Science (နိုင်ငံတကာ ကျောင်းတွေမှာ သင်တဲ့ ကွန်ပျူတာသိပ္ပံ သင်ရိုး) ရဲ့ syllabus (သင်ရိုး) မှာ B2.2.1 အပိုင်းက ဒီနှစ်မျိုးကို အောက်ပါ အချက်တွေအတိုင်း နှိုင်းယှဉ်ခိုင်းပါတယ်:

  Static (Array) Dynamic (ArrayList)
အရွယ်အစား ဖန်တီးတဲ့အချိန်မှာ ပုံသေ သတ်မှတ်ပြီးသား အလိုအလျောက် ကြီးလာ၊ သေးသွား
Memory (မှတ်ဉာဏ်) Memory နေရာ တစ်စုကို အစကတည်းက ကြိုယူထား လိုသလောက် နေရာ ယူ
မြန်နှုန်း Index (နေရာအမှတ်) နဲ့ တိုက်ရိုက် ယူလို့ ပိုမြန် Overhead (ပိုလုပ်ရတဲ့ အလုပ်) ကြောင့် နည်းနည်း ပိုနှေး
ပြောင်းလွယ်မှု အရွယ်အစားကို ကြိုသိထားရ စိတ်ကြိုက် ထည့်၊ ဖယ်လို့ရ
Syntax (code ရေးထုံး) int[] grades = new int[30]; ArrayList<Integer> grades = new ArrayList<>();

Hardware နဲ့ ဆက်စပ်ချက်: RAM (ပါဝါပိတ်ရင် ပျောက်တဲ့ အဓိက memory) ဘယ်လို အလုပ်လုပ်လဲ ပြန်သတိရပါ။ Memory ကို block (အကွက်လိုက် နေရာ) တွေနဲ့ ခွဲပေးပါတယ်။ Static array က (အကုန် မဖြည့်ရင်တောင်) ပုံသေ block တစ်ခုကို အစကတည်းက ယူထားပါတယ်။ ArrayList ကလည်း သူ့ item (အချက်) တွေကို block တစ်ခုထဲမှာပဲ သိမ်းပါတယ် (object (class တစ်ခုကနေ ဖန်တီးထားတဲ့ instance) ထဲမှာ array တစ်ခု ရှိပါတယ်)။ အဲဒီ block ပြည့်သွားရင် ပိုကြီးတဲ့ block အသစ် တစ်ခုကို ဖန်တီးပြီး item တွေကို အဲဒီထဲ copy ကူးထည့်ပါတယ်။ Array တွေက တန်ဖိုးတွေကို တိုက်ရိုက် သိမ်းထားလို့ copy ကူးစရာ မလိုသလို wrapper object (int လို အခြေခံ တန်ဖိုးကို ထုပ်ပိုးထားတဲ့ object) တွေလည်း မလိုတဲ့အတွက် နည်းနည်း ပိုမြန်ပါတယ်။ ဒါပေမယ့် ကြီးအောင် လုပ်လို့ မရပါဘူး။

ArrayList တစ်ခုကို ကြေညာခြင်း

import java.util.ArrayList;

ArrayList<String> names = new ArrayList<>();
ArrayList<Integer> scores = new ArrayList<>();
ArrayList<Double> prices = new ArrayList<>();
# No import and no element type to declare - one list type holds anything
names = []
scores = []
prices = []

အဓိက syntax:

  • java.util.ArrayList ကို import လုပ်ပါ
  • Element ရဲ့ type ကို ထောင့်ကွင်းထဲမှာ ရေးပါ: <String>၊ <Integer>၊ <Double>
  • Diamond operator (ထောင့်ကွင်း အလွတ် <>) နဲ့ new ArrayList<>() ကို သုံးပါ (type ကို Java က သူ့ဘာသာ သိပါတယ်)

Wrapper Types (primitive type ရဲ့ object ပုံစံများ)

ArrayList တွေက primitive type (Java ရဲ့ အခြေခံ ဒေတာ အမျိုးအစား) တွေကို မသိမ်းဘဲ object တွေကိုပဲ သိမ်းပါတယ်။ Java မှာ primitive type တစ်ခုစီအတွက် wrapper class (primitive type ရဲ့ object ပုံစံ) တစ်ခုစီ ရှိပါတယ်:

Primitive type Wrapper class
int Integer
double Double
boolean Boolean
char Character
ArrayList<Integer> scores = new ArrayList<>();  // Integer, not int
scores.add(95);   // Java auto-converts 95 to Integer (autoboxing)
int s = scores.get(0);  // Java auto-converts Integer to int (unboxing)
# Python has no wrapper classes - a list stores int, float, bool directly
scores = []
scores.append(95)   # no autoboxing needed
s = scores[0]       # no unboxing needed

Code ထဲက comment (ရှင်းလင်းချက် စာ) တွေမှာ ပါတဲ့ autoboxing ဆိုတာ int တန်ဖိုးကို Integer object အဖြစ် Java က အလိုအလျောက် ပြောင်းပေးတာ၊ unboxing ဆိုတာ Integer ကို int အဖြစ် ပြန်ပြောင်းပေးတာပါ။

ထောင့်ကွင်းထဲမှာ int မဟုတ်ဘဲ Integer ကို သုံးပါ။ ArrayList<int> လို့ ရေးရင် compile error (compile လုပ်ချိန်မှာ ဖြစ်တဲ့ error) ဖြစ်ပါတယ်။

ArrayList က object တွေကို သိမ်းတာကြောင့် wrapper class တွေ (int မဟုတ်ဘဲ Integer) လိုပါတယ်။ String က object ဖြစ်ပြီးသားမို့ ArrayList<String> မှာ wrapper မလိုပါဘူး။ Object နဲ့ primitive ကွာခြားချက်ကို OOP လေ့လာတဲ့အခါ ပိုလေ့လာရပါမယ်။


အဓိက လုပ်ဆောင်ချက်များ

Element ထည့်ခြင်း

ArrayList<String> fruits = new ArrayList<>();

fruits.add("Apple");       // adds to the end       -> [Apple]
fruits.add("Banana");      // adds to the end       -> [Apple, Banana]
fruits.add("Cherry");      // adds to the end       -> [Apple, Banana, Cherry]
fruits.add(1, "Mango");    // inserts at index 1    -> [Apple, Mango, Banana, Cherry]
fruits = []

fruits.append("Apple")      # adds to the end       -> [Apple]
fruits.append("Banana")     # adds to the end       -> [Apple, Banana]
fruits.append("Cherry")     # adds to the end       -> [Apple, Banana, Cherry]
fruits.insert(1, "Mango")   # inserts at index 1    -> [Apple, Mango, Banana, Cherry]

add(element) က list ရဲ့ အဆုံးမှာ ထပ်ထည့်ပါတယ်။ add(index, element) က သတ်မှတ်ထားတဲ့ နေရာမှာ ထည့်သွင်းပြီး ရှိပြီးသား element တွေကို ညာဘက်ကို ရွှေ့ပေးပါတယ်။

မထည့်ခင်: Apple က index 0၊ Banana က 1၊ Cherry က 2 မှာ ရှိပါတယ်။ add(1, "Mango") ပြီးနောက်: Apple က 0၊ Mango က 1 မှာ ရှိပြီး Banana က 1 ကနေ 2 ကို၊ Cherry က 2 ကနေ 3 ကို ရွှေ့သွားပါတယ်။ fruits.add(1, "Mango"); fruits.insert(1, "Mango") before Apple 0 Banana 1 Cherry 2 after Apple 0 Mango 1 Banana 2 Cherry 3

ပုံထဲက before ဆိုတာ မထည့်ခင်၊ after ဆိုတာ ထည့်ပြီးနောက်လို့ ဆိုလိုပါတယ်။ အကွက် တစ်ကွက်စီရဲ့ အောက်က ဂဏန်းက index ပါ။

Element ကို ယူသုံးခြင်း

String first = fruits.get(0);    // "Apple"
String second = fruits.get(1);   // "Mango"
first = fruits[0]    # "Apple" - Python lists DO use square brackets (no get())
second = fruits[1]   # "Mango"

[index] မဟုတ်ဘဲ .get(index) ကို သုံးပါ။ လေးထောင့်ကွင်း [ ] နဲ့ ယူသုံးတဲ့ နည်းက ArrayList မှာ အလုပ် မလုပ်ပါဘူး။

Element ကို ပြင်ခြင်း

fruits.set(1, "Pear");   // replaces index 1 -> [Apple, Pear, Banana, Cherry]
fruits[1] = "Pear"   # replaces index 1 -> [Apple, Pear, Banana, Cherry]

Element ကို ဖယ်ထုတ်ခြင်း

fruits.remove(2);           // removes by index -> [Apple, Pear, Cherry]
fruits.remove("Cherry");    // removes by value -> [Apple, Pear]
fruits.pop(2)             # removes by index -> [Apple, Pear, Cherry]
fruits.remove("Cherry")   # removes by value -> [Apple, Pear]
မဖယ်ခင်: Apple က index 0၊ Pear က 1၊ Banana က 2၊ Cherry က 3 မှာ ရှိပါတယ်။ remove(2) က Banana ကို ဖယ်ထုတ်ပါတယ် (မျဉ်းပြတ်နဲ့ ပြထားပါတယ်)။ Cherry က index 3 ကနေ index 2 ကို ရွှေ့သွားပါတယ်။ အခု list ထဲမှာ item သုံးခု ရှိပါတယ်။ fruits.remove(2); fruits.pop(2) before Apple 0 Pear 1 Banana 2 Cherry 3 after Apple 0 Pear 1 Cherry 2

ပုံထဲက before ဆိုတာ မဖယ်ခင်၊ after ဆိုတာ ဖယ်ပြီးနောက်လို့ ဆိုလိုပါတယ်။

Element တစ်ခုကို ဖယ်ထုတ်ရင် အဲဒီ element နောက်က element အားလုံး ဘယ်ဘက်ကို ရွှေ့သွားပါတယ်။ နောက်က element တွေရဲ့ index တွေ ပြောင်းသွားပါတယ်။

Size (element အရေအတွက်)

int count = fruits.size();   // 2
count = len(fruits)   # 2 - len() covers lists and strings alike in Python

.length မဟုတ်ဘဲ .size() ကို သုံးပါ။ အောက်က သုံးမျိုး ရောထွေးတတ်တဲ့ ထောင်ချောက်ကို သတိထားပါ:

Structure အရေအတွက်ကို ယူပုံ
Array arr.length (ကွင်း မပါ)
ArrayList list.size() (ကွင်း ပါ)
String str.length() (ကွင်း ပါ)

List ထဲမှာ ပါမပါ စစ်ခြင်း

boolean hasPear = fruits.contains("Pear");   // true
int pos = fruits.indexOf("Apple");           // 0 (or -1 if not found)
has_pear = "Pear" in fruits    # True - the in keyword checks membership
pos = fruits.index("Apple")    # 0 - but raises an error if not found (no -1)

String method (သီးခြား အလုပ်တစ်ခု လုပ်ပေးတဲ့ code အပိုင်း) တွေနဲ့ ဆက်စပ်ချက်: indexOf() က ရှာမတွေ့ရင် -1 ကို ပြန်ပေးပါတယ်၊ String.indexOf() နဲ့ စည်းမျဉ်း အတူတူပါပဲ။ contains() က boolean (true ဒါမှမဟုတ် false တန်ဖိုးပဲ ယူတဲ့) တန်ဖိုး တစ်ခုကို ပြန်ပေးပါတယ်၊ ကိုက်ညီမှု ရှိမရှိ စစ်ဖို့ String.equals() ကို သုံးပုံနဲ့ ဆင်တူပါတယ်။


ArrayList ကို Traverse လုပ်ခြင်း

ရိုးရိုး for loop

for (int i = 0; i < names.size(); i++) {
    System.out.println(names.get(i));
}
for i in range(len(names)):
    print(names[i])

Enhanced for loop (ဖတ်တတ်အောင် သိထားပါ၊ ရေးတဲ့အခါ index သုံးတဲ့ loop ကို ရေးပါ)

Java မှာ index မပါဘဲ element တစ်ခုချင်းစီကို အစဉ်လိုက် ပေးတဲ့ ပိုတိုတဲ့ “for-each” loop (ထပ်ခါထပ်ခါ run တဲ့ code အပိုင်း) လည်း ရှိပါတယ်။ Index သုံးတဲ့ for loop တွေက စိတ်ချရပါတယ်။ For-each က list တွေမှာ သုံးလို့ရပေမယ့် မကြာခဏ လိုအပ်တဲ့ index ကို မရတော့ပါဘူး။ For-each ကို တခြားသူတွေ ရေးတဲ့ code ထဲမှာ တွေ့ရမှာမို့ ဖတ်တတ်ဖို့ လိုပါတယ်။ ဒါပေမယ့် အပေါ်က index သုံးတဲ့ loop က ဒီ site တစ်ခုလုံးမှာ သုံးတဲ့ ပုံစံ ဖြစ်ပြီး IB ရဲ့ ကိုယ်ပိုင် စာမေးပွဲ မေးခွန်းစာရွက်တွေမှာ ရေးတဲ့ ပုံစံလည်း ဖြစ်ပါတယ်။ For-each ပုံစံကို မှတ်မိအောင် ဒီမှာ တစ်ကြိမ်ပဲ comment အဖြစ် ပြထားပါတယ်:

for (int i = 0; i < names.size(); i++) {
    String name = names.get(i);
    System.out.println(name);
}
// for-each version: for (String name : names) { System.out.println(name); }
for i in range(len(names)):
    name = names[i]
    print(name)
# for-each version: for name in names:

For-each loop က ပိုတိုပေမယ့် loop ထဲမှာ index ကို မမြင်ရပါဘူး။ Loop run နေစဉ်မှာ element တွေကို ထည့်တာ၊ ဖယ်ထုတ်တာလည်း မလုပ်နိုင်ပါဘူး (Java က များသောအားဖြင့် error နဲ့ program ကို ရပ်လိုက်ပါတယ်။ တချို့ အခြေအနေတွေမှာတော့ loop က ဘာမှ မပြောဘဲ စောစော ပြီးသွားပြီး element တွေကို ကျော်သွားပါတယ်)။ ဒီအချက် နှစ်ခုလုံးက နောက် အပိုင်းမှာ အရေးကြီးပါတယ်။

Traverse လုပ်နေစဉ် element ဖယ်ထုတ်ခြင်း

ရှေ့ကို loop ပတ်နေရင်း element တွေကို ဖယ်ထုတ်ရင် index တွေ ရွှေ့သွားလို့ ပြဿနာ ဖြစ်ပါတယ်။ အဲဒီအစား နောက်ပြန် loop ပတ်ပါ:

for (int i = names.size() - 1; i >= 0; i--) {
    if (names.get(i).equals("Bob")) {
        names.remove(i);
    }
}
# Count down with a while loop; == compares string content in Python
i = len(names) - 1
while i >= 0:
    if names[i] == "Bob":
        names.pop(i)
    i = i - 1

Index ကို မညှိဘဲ ရှေ့သွား loop ထဲမှာ element တွေကို ဘယ်တော့မှ မဖယ်ထုတ်ပါနဲ့။ Element တွေကို ကျော်သွားမိမယ်၊ ဒါမှမဟုတ် IndexOutOfBoundsException (index က list ရဲ့ အပိုင်းအခြားကို ကျော်သွားလို့ program run နေစဉ် ဖြစ်တဲ့ အမှား) ဖြစ်ပါလိမ့်မယ်။


အဆင့်ဆင့် ရှင်းပြထားတဲ့ ဥပမာများ

ဥပမာ 1: ကျောင်းသား စာရင်း

ကျောင်းသားတွေ အသစ် ဝင်လာတာ၊ ထွက်သွားတာနဲ့အမျှ ပြောင်းလဲနေတဲ့ အတန်း စာရင်းကို ဆရာ တစ်ယောက်က စီမံဖို့ လိုပါတယ်။

import java.util.ArrayList;

ArrayList<String> students = new ArrayList<>();

// Students enroll
students.add("Alice");
students.add("Bob");
students.add("Charlie");
students.add("Diana");

System.out.println("Enrolled: " + students.size());  // 4

// Bob transfers out
students.remove("Bob");
System.out.println("After transfer: " + students.size());  // 3

// New student joins
students.add("Eve");

// Print the roster
for (int i = 0; i < students.size(); i++) {
    System.out.println((i + 1) + ". " + students.get(i));
}
students = []   # a plain Python list - no import needed

# Students enroll
students.append("Alice")
students.append("Bob")
students.append("Charlie")
students.append("Diana")

print("Enrolled:", len(students))  # 4

# Bob transfers out
students.remove("Bob")
print("After transfer:", len(students))  # 3

# New student joins
students.append("Eve")

# Print the roster
for i in range(len(students)):
    print(str(i + 1) + ". " + students[i])

Output (program က ထုတ်ပြတဲ့ ရလဒ်):

Enrolled: 4
After transfer: 3
1. Alice
2. Charlie
3. Diana
4. Eve

ဥပမာ 2: List ကို စစ်ထုတ်ခြင်း

စာမေးပွဲ ရလဒ် စာရင်း တစ်ခုထဲက 80 ထက် ပိုတဲ့ အမှတ် အားလုံးကို ရှာပါ။

public static ArrayList<Integer> getHighScores(ArrayList<Integer> scores) {
    ArrayList<Integer> high = new ArrayList<>();
    for (int i = 0; i < scores.size(); i++) {
        if (scores.get(i) > 80) {
            high.add(scores.get(i));
        }
    }
    return high;
}
def get_high_scores(scores):
    high = []
    for i in range(len(scores)):
        if scores[i] > 80:
            high.append(scores[i])
    return high

ဥပမာ 3: ပျမ်းမျှ တွက်ခြင်း

public static double getAverage(ArrayList<Integer> scores) {
    if (scores.size() == 0) {
        return 0;
    }
    int total = 0;
    for (int i = 0; i < scores.size(); i++) {
        total += scores.get(i);
    }
    return (double) total / scores.size();
}
def get_average(scores):
    if len(scores) == 0:
        return 0
    total = 0
    for i in range(len(scores)):
        total = total + scores[i]
    return total / len(scores)   # / always gives a decimal - no cast needed

Array နဲ့ ArrayList နှိုင်းယှဉ်ချက်

အချက် Array ArrayList
အရွယ်အစား ဖန်တီးတဲ့အချိန်မှာ ပုံသေ လိုသလို ကြီးလာ၊ သေးသွား
ကြေညာပုံ int[] arr = new int[5]; ArrayList<Integer> list = new ArrayList<>();
ယူသုံးပုံ arr[i] list.get(i)
ပြင်ပုံ arr[i] = value; list.set(i, value)
Length/Size (အရေအတွက်) arr.length list.size()
Element ထည့်ခြင်း မလုပ်နိုင်ပါ (ပုံသေ) list.add(value)
Element ဖယ်ထုတ်ခြင်း မလုပ်နိုင်ပါ (ပုံသေ) list.remove(index)
Primitive တွေကို သိမ်းနိုင်လား သိမ်းနိုင်ပါတယ် မသိမ်းနိုင်ပါ (wrapper class တွေကို သုံးပါတယ်)
စွမ်းဆောင်ရည် ပိုမြန် (memory ကို တိုက်ရိုက် ယူသုံး) နည်းနည်း ပိုနှေး (object overhead)

မှတ်ထားရလွယ်တဲ့ စည်းမျဉ်း: အရွယ်အစားကို သိပြီး ပုံသေ ဖြစ်တဲ့အခါ array ကို သုံးပါ။ အရွယ်အစား ပြောင်းလဲတဲ့အခါ ArrayList ကို သုံးပါ။


ကြိုတင် ကြည့်ခြင်း: Object တွေ သိမ်းတဲ့ ArrayList

ဒီအပိုင်းက နောက်မှ လေ့လာမယ့် အကြောင်းအရာကို ကြိုပြတာပါ။ Class (object တွေ ဖန်တီးဖို့ ပုံစံခွက်) နဲ့ object တွေအကြောင်းကို OOP လေ့လာတဲ့အခါ သင်ရပါမယ်။ အခုအတွက်တော့ ArrayList က String နဲ့ Integer ကိုပဲ သိမ်းနိုင်တာ မဟုတ်ဘူးဆိုတာကိုပဲ သတိပြုထားပါ။

OOP ကို လေ့လာတဲ့အခါ ကိုယ်ပိုင် class တွေကို ရေးပြီး အဲဒီ class တွေရဲ့ object တွေကို ArrayList ထဲမှာ သိမ်းပါမယ်:

ArrayList<Student> roster = new ArrayList<>();
roster.add(new Student("Alice", 90));
roster.add(new Student("Bob", 85));

// Find a student by name
for (int i = 0; i < roster.size(); i++) {
    if (roster.get(i).getName().equals("Alice")) {
        System.out.println("Found: " + roster.get(i).getGrade());
    }
}
roster = []
roster.append(Student("Alice", 90))   # no new keyword in Python
roster.append(Student("Bob", 85))

# Find a student by name
for i in range(len(roster)):
    if roster[i].get_name() == "Alice":
        print("Found:", roster[i].get_grade())

ဒါက object တွေ သိမ်းတဲ့ array တွေရဲ့ ပုံစံအတိုင်းပါပဲ၊ [i] အစား get() ကို သုံးတာပဲ ကွာပါတယ်။ .size() က လက်ရှိ element အရေအတွက်ကို ပြန်ပေးတာကြောင့် count variable (တန်ဖိုး သိမ်းထားတဲ့ နာမည်ပေးထားတဲ့ နေရာ) သီးသန့် မလိုပါဘူး။


Quick Check (အမြန် စစ်ဆေးမေးခွန်း)

Q1. ArrayList<int> က ဘာကြောင့် compile error ဖြစ်သလဲ။

Q2. items လို့ နာမည်ပေးထားတဲ့ ArrayList ထဲက element အရေအတွက်ကို ဘယ်လို ယူမလဲ။

Q3. list = [Apple, Banana, Cherry] ဖြစ်ရင် list.remove(1) ပြီးနောက် list က ဘာဖြစ်သွားမလဲ။

Q4. list = [Apple, Banana] ဖြစ်ရင် list.add(1, "Mango") ပြီးနောက် list က ဘာဖြစ်သွားမလဲ။

Q5. ArrayList ထဲက element တွေကို ဖယ်ထုတ်တဲ့အခါ ဘာကြောင့် နောက်ပြန် loop ပတ်သင့်သလဲ။


Trace Exercise (code ကို တစ်ဆင့်ချင်း လိုက်ကြည့်ခြင်း)

Operation (လုပ်ဆောင်ချက်) တစ်ခုစီ ပြီးတိုင်း ArrayList ရဲ့ အခြေအနေကို trace လုပ်ပါ။

Trace: Line တစ်ကြောင်းစီ ပြီးတိုင်း ArrayList ထဲက တန်ဖိုးတွေနဲ့ size (element အရေအတွက်) ကို မှတ်ပါ။ List ထဲက တန်ဖိုးတွေကို ဇယားထဲက ပုံစံအတိုင်း English လို ရိုက်ထည့်ပါ။

ArrayList<String> list = new ArrayList<>();
list.add("Red");       // Line 1
list.add("Blue");      // Line 2
list.add("Green");     // Line 3
list.add(1, "Yellow"); // Line 4
list.remove(2);        // Line 5
list.set(0, "Purple"); // Line 6
</tbody>
Line ပြီးနောက်List ထဲက တန်ဖိုးများSize
1 [Red] 1
2 [Red, Blue] 2
3 [Red, Blue, Green]
4
5
6

Code Completion (code ကွက်လပ် ဖြည့်ခြင်း)

ArrayList ထဲက ကျရှုံးတဲ့ အမှတ် (50 အောက်) အားလုံးကို ဖယ်ထုတ်ပေးတဲ့ method ကို ဖြည့်ပါ။

ကွက်လပ် ဖြည့်ပါ: ကျရှုံးတဲ့ အမှတ် (50 အောက်) ရထားတဲ့ ကျောင်းသား အားလုံးကို ဖယ်ထုတ်ပါ။

public static void removeFailing(ArrayList<Integer> grades) {
    for (int i = grades. - 1; i >= 0; i--) {
        if (grades. < 50) {
            grades.;
        }
    }
}

Spot the Error (အမှား ရှာခြင်း)

ဒီ code က list ထဲက နာမည်အားလုံးကို print ထုတ်ရမှာ ဖြစ်ပေမယ့် compile မဖြစ်ပါဘူး။

Bug ရှာပါ: ဒီ code က compile မဖြစ်ပါဘူး။ အမှားကို ရှာပါ။

1ArrayList<String> names = new ArrayList<>(); 2for (int i = 0; i < names.length; i++) { 3 System.out.println(names.get(i)); 4}

Line 2 အတွက် မှန်ကန်တဲ့ ပြင်နည်းကို ရွေးပါ:

Bug ရှာပါ: ဒီ line က compile မဖြစ်ပါဘူး။ အမှားကို ရှာပါ။

1ArrayList<int> numbers = new ArrayList<>(); 2numbers.add(42); 3numbers.add(17);

Line 1 အတွက် မှန်ကန်တဲ့ ပြင်နည်းကို ရွေးပါ:

Bug ရှာပါ: ဒီ code က element အားလုံးကို ဖယ်ထုတ်ဖို့ ကြိုးစားပေမယ့် list ထဲမှာ [B] ကျန်နေပါတယ်။ အမှားကို ရှာပါ။

1ArrayList<String> list = new ArrayList<>(); 2list.add("A"); list.add("B"); list.add("C"); 3for (int i = 0; i < list.size(); i++) { 4 list.remove(i); 5}

Line 3 အတွက် မှန်ကန်တဲ့ ပြင်နည်းကို ရွေးပါ:


Predict the Output (output ကို ကြိုခန့်မှန်းခြင်း)

ကြိုခန့်မှန်းပါ: ဒီ code က ဘာကို print ထုတ်မလဲ။

ArrayList<Integer> nums = new ArrayList<>();
nums.add(10);
nums.add(20);
nums.add(30);
nums.add(1, 40);
nums.remove(2);
System.out.println(nums.get(1));

ကြိုခန့်မှန်းပါ: ဒီ code က ဘာကို print ထုတ်မလဲ။

ArrayList<String> names = new ArrayList<>();
names.add("Apple");
names.add("Banana");
names.add("Cherry");
for (int i = names.size() - 1; i >= 0; i--) {
    System.out.println(names.get(i));
}

Practice Exercises (လေ့ကျင့်ခန်းများ)

[5 marks] စတာတွေက မေးခွန်း တစ်ခုစီ (ဒါမှမဟုတ် အပိုင်း တစ်ခုစီ) အတွက် ရနိုင်တဲ့ အမှတ်ပါ။ IB စာမေးပွဲ မေးခွန်းတွေမှာ အမှတ်ကို ဒီလို ပြလေ့ ရှိပါတယ်။

Core (အခြေခံ အဆင့်)

  1. ဂဏန်း စုဆောင်းခြင်း။ User က -1 ကို ရိုက်ထည့်တဲ့အထိ integer (ကိန်းပြည့်) တွေကို ဖတ်တဲ့ program ကို Construct (ကိုယ်တိုင် တည်ဆောက်ပါ)။ ဂဏန်းတွေကို ArrayList<Integer> ထဲမှာ သိမ်းပါ။ ပေါင်းလဒ်၊ ပျမ်းမျှ၊ အကြီးဆုံး တန်ဖိုးနဲ့ အငယ်ဆုံး တန်ဖိုးကို print ထုတ်ပါ။ [5 marks]

  2. စကားလုံး စစ်ထုတ်ခြင်း။ minLength character (စာလုံး ဒါမှမဟုတ် သင်္ကေတ) ထက် တိုတဲ့ စကားလုံး အားလုံးကို ဖယ်ထုတ်ပေးတဲ့ method filterShort(ArrayList<String> words, int minLength) ကို Construct လုပ်ပါ။ စစ်ထုတ်ပြီးသား list ကို print ထုတ်ပါ။ [4 marks]

  3. အမှတ် မှတ်တမ်း။ ပျမ်းမျှကို double အဖြစ် return (ရလဒ်အဖြစ် ပြန်ပေး) လုပ်တဲ့ method getAverage(ArrayList<Integer> scores) ကို Construct လုပ်ပါ။ [3 marks]

Extension (တိုးချဲ့ အဆင့်)

  1. မထပ်တဲ့ list။ User ဆီကနေ string (စာသား) တွေကို ဖတ်တဲ့ program ကို Construct လုပ်ပါ။ String တစ်ခုကို list ထဲမှာ မရှိသေးမှသာ ထည့်ပါ (.contains() ကို သုံးပါ)။ မထပ်တဲ့ item တွေပါတဲ့ နောက်ဆုံး list ကို print ထုတ်ပါ။ [4 marks]

  2. Reverse method မသုံးဘဲ ပြောင်းပြန်လှန်ခြင်း။ ArrayList တစ်ခုကို လက်ခံပြီး element တွေကို ပြောင်းပြန် အစဉ်နဲ့ စီထားတဲ့ ArrayList အသစ် တစ်ခုကို return လုပ်တဲ့ method ကို Construct လုပ်ပါ။ Collections.reverse ကို မသုံးပါနဲ့။ [4 marks]

Challenge (စိန်ခေါ်မှု အဆင့်)

  1. Sorted list နှစ်ခုကို ပေါင်းခြင်း။ Sorted (တန်ဖိုးအလိုက် အစဉ်တကျ စီပြီးသား) ArrayList<Integer> နှစ်ခုကို လက်ခံပြီး နှစ်ခုလုံးက element အားလုံး ပါတဲ့ sorted ArrayList တစ်ခုကို return လုပ်တဲ့ method ကို Construct လုပ်ပါ။ ဘယ် sort method ကိုမှ မသုံးပါနဲ့။ [6 marks]

Exam-Style (စာမေးပွဲ ပုံစံ မေးခွန်း၊ 16 marks)

ဆရာ တစ်ယောက်က ကျောင်းသား နာမည်တွေကို ArrayList<String> တစ်ခုထဲမှာ၊ သူတို့ရဲ့ အမှတ်တွေကို parallel (index တူတဲ့ နေရာတွေမှာ ဆက်စပ်တဲ့ ဒေတာ သိမ်းထားတဲ့) ArrayList<Integer> တစ်ခုထဲမှာ သိမ်းထားပါတယ် (index တူရင် ကျောင်းသား တစ်ယောက်တည်း ဖြစ်ပါတယ်)။

(a) ကျောင်းသား တစ်ယောက်နဲ့ သူ့အမှတ်ကို list တွေထဲ ထည့်ပေးတဲ့ method addStudent(ArrayList<String> names, ArrayList<Integer> scores, String name, int score) ကို Construct လုပ်ပါ။ [2]

(b) အတန်းရဲ့ ပျမ်းမျှ အမှတ်ကို double အဖြစ် return လုပ်တဲ့ method getAverage(ArrayList<Integer> scores) ကို Construct လုပ်ပါ။ [3]

(c) ပျမ်းမျှထက် ပိုရတဲ့ ကျောင်းသားတွေရဲ့ နာမည်တွေကိုပဲ ပါတဲ့ ArrayList<String> အသစ် တစ်ခုကို return လုပ်တဲ့ method getAboveAverage(ArrayList<String> names, ArrayList<Integer> scores) ကို Construct လုပ်ပါ။ [4]

(d) ဒီ program အတွက် array အစား ArrayList ကို သုံးရတဲ့ အားသာချက် တစ်ခုကို Explain (ရှင်းပြပါ)။ [1]

(e) ပေးထားတဲ့ ကန့်သတ် အမှတ် (threshold) အောက် ရတဲ့ ကျောင်းသား အားလုံးကို ArrayList နှစ်ခုလုံးကနေ ဖယ်ထုတ်ဖို့ လိုအပ်တဲ့ အဆင့်တွေကို Describe (ဖော်ပြရှင်းပြပါ)။ Code ရေးဖို့ မလိုပါဘူး။ [4]

(f) ArrayList ထက် ရိုးရိုး array က ပိုသင့်တော်မယ့် အခြေအနေ တစ်ခုကို Outline (အကျဉ်းချုပ် ဖော်ပြပါ)။ [2]


GitHub Classroom (အလိုအလျောက် စစ်ဆေးတဲ့ test တွေပါတဲ့ programming assignment များ)

ArrayList Exercises
Practice ArrayList operations: number collector, word filter, score tracker, unique list, reverse, merge sorted, and a multi-part grade tracker exam-style question.

A class assignment on GitHub Classroom: use it when your teacher has set it. It needs a free GitHub account.

Working in the Codespace

  1. Accept and open. Accept the assignment above, open the link in the email from GitHub, then press Open in Codespace. In Java, change the status bar from Lightweight to Standard mode.
  2. Write and check. Open src/NumberCollector.java and complete each TODO using ArrayList<Integer> and a sentinel loop. Press Run and compare the output with the comments, or run bash run-tests.sh in the Terminal.
  3. Submit. In Source Control, write a message, press Commit, then Sync Changes. Your teacher’s feedback arrives as a pull request called Feedback.

There are 7 exercise files in src/: start with NumberCollector.java and work through the tiers.

The full Codespace guide, with pictures


ဆက်စပ် ခေါင်းစဉ်များ

  • အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: 1D Arrays (တန်းတစ်တန်းတည်းမှာ စီထားတဲ့ ဒေတာအစုများ): အရွယ်အစား ပုံသေ array တွေနဲ့ traverse လုပ်ခြင်း
  • အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: Strings (String နဲ့ သုံးတဲ့ method များ): element တွေကို နှိုင်းယှဉ်ဖို့ .equals()၊ ရှာမတွေ့ရင် -1 ကို ပြန်ပေးတဲ့ indexOf()၊ length() နဲ့ size() ကွာခြားချက်
  • အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: Methods (method တွေ ရေးပြီး program ကို အပိုင်းငယ်တွေ ခွဲတည်ဆောက်ခြင်း): ArrayList method တွေကို ခေါ်ပုံကလည်း အတူတူပါပဲ: parameter (method ကို ပေးလိုက်တဲ့ တန်ဖိုးကို လက်ခံတဲ့ variable) တွေနဲ့ return value (ပြန်ပေးတဲ့ တန်ဖိုး) တွေ
  • ဆက်စပ် ခေါင်းစဉ်: Primary Memory (ကွန်ပျူတာရဲ့ အဓိက memory): static နဲ့ dynamic memory allocation (memory နေရာ ချပေးခြင်း)၊ RAM block များ
  • ဆက်စပ် ခေါင်းစဉ်: Exception Handling (program run နေစဉ် ဖြစ်တဲ့ အမှားတွေကို ကိုင်တွယ်ခြင်း): index မမှန်ရင် ဖြစ်တဲ့ IndexOutOfBoundsException
  • နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်: File Processing (file တွေကို ဖတ်ခြင်းနဲ့ ရေးခြင်း): file တစ်ခုထဲက အရေအတွက် မသိတဲ့ line တွေကို ArrayList ထဲ ဖတ်ထည့်ခြင်း
  • နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်: OOP Aggregation (သီးခြား ရှိနေနိုင်တဲ့ has-a ဆက်နွယ်မှု): container class (တခြား object တွေကို စုသိမ်းထားတဲ့ class) ထဲမှာ object တွေ သိမ်းတဲ့ ArrayList
  • နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်: Stacks (အပေါ်ကနေပဲ ထည့်၊ ထုတ်တဲ့ အထပ်များ): Stack ကလည်း ဆင်တူတဲ့ CRUD (Create၊ Read၊ Update၊ Delete: ထည့်၊ ဖတ်၊ ပြင်၊ ဖျက်) ပုံစံတွေကို သုံးပါတယ်
  • နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်: Queues (အရင်ထည့်တာ အရင်ထွက်တဲ့ တန်းများ): Queue ကလည်း ဆင်တူတဲ့ CRUD ပုံစံတွေကို သုံးပါတယ်

© EduCS.me. ကွန်ပျူတာသိပ္ပံ ပညာရေးအတွက် အရင်းအမြစ်တွေ စုထားတဲ့ နေရာပါ။ EduCS is an independent educational resource. It is not affiliated with, or endorsed by, the International Baccalaureate Organization. (EduCS ဟာ သီးခြား ပညာရေး အရင်းအမြစ်တစ်ခုပါ။ International Baccalaureate Organization နဲ့ ဆက်စပ်မှု မရှိသလို သူတို့ရဲ့ ထောက်ခံချက်လည်း မရှိပါဘူး။) ဆက်သွယ်ရန်