Thuta Learning
ရှာဖွေရန်
BasicProgrammingbeginner

Arrays

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Array သည် ordered list ဖြစ်ပါတယ်။ Item တွေကို နံပါတ် index နဲ့သိမ်းထားပြီး index က 0 ကနေစပါတယ်။ Shopping list, users list, scores list စတာတွေသိမ်းဖို့ အသုံးများပါတယ်။

ruby
fruits = ["Apple", "Banana", "Cherry"]

puts fruits[0]
fruits << "Durian"
puts fruits.last
puts "Total fruits: #{fruits.length}"

fruits.each_with_index do |fruit, index|
  puts "#{index + 1}. #{fruit}"
end

fruits[0] က ပထမ item ကိုယူပါတယ်။ << က array အဆုံးမှာ item ထည့်ပါတယ်။ .each_with_index က item နဲ့ index နှစ်ခုလုံးကို loop ထဲပေးပါတယ်။

You should see
Apple Durian Total fruits: 4 1. Apple 2. Banana 3. Cherry 4. Durian

Info

Array index က 1 မဟုတ်ဘဲ 0 ကစတာ သတိထားပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • fruits[10] လို မရှိတဲ့ index ကိုခေါ်ရင် error မပေးဘဲ nil ပြန်နိုင်ပါတယ်။ အဲ့ဒီ nil ကို method ဆက်ခေါ်ရင် error ဖြစ်နိုင်ပါတယ်။
Arrays | Thuta Learning