สมัครเรียนโทร. 085-350-7540 , 084-88-00-255 , ntprintf@gmail.com

Tutorial DART

L01 DART INTRO L02 DART HOWTO L03 DART GETTING START L04 DART SYNTAX L05 DART VARIABLE 01 L06 DART VARIABLE 02 L07 DART FUNCTION L08 DART OPTIONAL NAMED POSITONAL L09 DART LIST L10 DART CLASS L11 DART INHERITANCE MIXIN L12 DART EXCEPTION L13 DART GENERATOR AND ITERATOR L14 DART OPERATION

การใช้ List (fixed - length,Growable)

เรียนเขียนโปรแกรมง่ายๆกับ Expert programming Tutor ในบท การใช้ list (fixed -length,Growable)
การสร้างตัว list ข้อมูล ก็เหมือนกับการที่เราเก็บข้อมูลของ Arrays เวลาที่เรารับค่ามาเยอะๆเราก็ต้องการเก็บค่าไว้ใน arrays หรือ list มี 2 แบบ คือ 
- List แบบ fixed เราจะไม่สามารถเพิ่มค่าได้อีก และจะไม่สามารถใช้ฟังก์ชัน add() และ remove() ได้ เช่นในตัวอย่างเรา fixed ไว้เเค่ 3 มันก็ไม่สามารถเพิ่มค่าตัวที่ 4 ได้ 
ตัวอย่าง
เช่นในตัวอย่างเรา fixed ไว้เเค่ 3 มันก็ไม่สามารถเพิ่มค่าตัวที่ 4 ได้  เราจะทำการใส่ค่าเข้าไปเเละเก็บค่าไว้ใน list 

//List
List list1 = List(3);
Iist[0] = 1111;
list[1] = 22;
list[2] = 333;
print("list1: $list1");
}

ผลลัพธ์

list1: [1111, 22, 333]

ถ้าเราต้องการจะ access เข้าไป เดี๋ยวเราจะลองใส่ตัว[2]  ก็จะเข้าถึงค่าตัวอย่างด้านบนเป็น index ของมัน เเละครอบด้วยปีกกา{} ในรูปแบบ arrays list เราสามารถเปลี่ยนเเปลงค่าได้ 
ตัวอย่าง

//List
List list1 = List(3);
Iist1[0] = 1111;
list1[1] = 22;
list1[2] = 333;
//list[3] = 999;

print("list1: ${list1[2]}");
list1[2] = null;
print("list1: ${list1[2]}");
}

ผลลัพธ์ 

list1: 333

เปลี่ยนแปลงค่าได้เรื่อยๆเเต่ว่ามันจะเพิ่มไม่สามารถความยาวได้เเล้ว ต่อมาแบบไม่ fixed
ตัวอย่าง
ประกาศค่าแบบเราจะไม่ใส่ค่าเข้าไป เเละเรียกใช้ add () ได้ โดยไม่จำกัด เเละสามารถ remove() ได้

//List
List list1 = List(3);
Iist1[0] = 1111;
list1[1] = 22;
list1[2] = 333;
//list[3] = 999;
print("list1: ${list1[2]}");
list1[2] = null;
print("list1: ${list1[2]}");
List List2 = List();
list2[3] = 999;
print("list2: ${list2[3]}");

ผลลัพธ์ 

list1: 333
list1: null
list2: 213
list2: 999

เราได้เข้าถึงค่าตัวที่ 3 คือ 213 และทำการเปลี่ยนแปลงค่าจาก 213 เป็น 999 จากนั้นใช้ print ค่าออกมา ถ้าเราต้องการสร้างแบบ fixed และเปลี่ยนแปลงค่าไม่ได้ เราจะต้องใช้คีย์เวิร์ด const เข้ามาช่วย 
ตัวอย่าง

//List
List list1 = List(3);
list1[0] = 1111;
list1[1] = 22;
list1[2] = 333;
//list[3] = 999;

print("list1: ${list1[2]}");
list1[2] = null;
print("list1: ${list1[2]}");

List list2 = List();
list2.add(5555);
list2.add(435);
list2.add(123);
list2.add(213);
print("list2: ${list2[3]}");
list1[3] = 999;
print("list1: ${list1[2]}");
List List3 = const ["a", "b", "c"];
print("list3: ${list3[0]}");
}

ผลลัพธ์ 

list1: 333
list1: null
list2: 213
list2: 999
[a, b, c]: a

const คือเป็นค่าคงที่เเละไม่สามารถเเก้ไขค่าได้ 
สรุปเนื้อหาในบทนี้
1.list แบบ fixed ความยาว
2.list แบบ dynamic ความยาว
3.การสร้างตัวlist ข้อมูลแบบค่าคงที่ไม่สามารถเปลี่ยนแปลงค่าได้ เเละfixed ความยาว



บทความนี้อาจจะมีที่ผิด กรุณาตรวจสอบก่อนใช้

หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor

ไม่อยากอ่าน Tutorial อยากมาเรียนเลยทำอย่างไร?

สมัครเรียน ONLINE ได้ทันทีที่ https://elearn.expert-programming-tutor.com

หรือติดต่อ

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM

แผนที่ ที่ตั้งของอาคารของเรา

C Article


C++ Article


Java Article


C#.NET Article


VB.NET Article


Python Article


Golang Article


JavaScript Article


Perl Article


Lua Article


Rust Article


Article


Python


Python Numpy


Python Machine Learning



แผนผังการเรียนเขียนโปรแกรม

Link อื่นๆ

Allow sites to save and read cookie data.
Cookies are small pieces of data created by sites you visit. They make your online experience easier by saving browsing information. We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies.

Copyright (c) 2013 expert-programming-tutor.com. All rights reserved. | 085-350-7540 | 084-88-00-255 | ntprintf@gmail.com

ติดต่อเราได้ที่

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM
แผนที่ ที่ตั้งของอาคารของเรา