การใช้งาน Arrayarray เป็นกลุ่มของข้อมูลที่มีชนิดเดียวกันโดย สิ่งที่สำคัญเกี่ยวกับอาร์เรย์ก็คือตำแหน่งของอาร์เรย์ข้อมูลตัวแรก
ของอาร์เรย์จะเริ่มต้นที่ตำแหน่งเท่ากับ 0 เสมอดังนี้
data1,data2,data3
ถ้าเราอ้างอิงถึง data1 หมายความว่าเรากำลังอ้างอิงที่ตำแหน่งที่ 0 ส่วน data2 เป็นตำแหน่งที่ 1,...
รูปแบบการประกาศอาร์เรย์
array<array_type>^ arrayname;
เช่น
array<int>^ i; โดยที่ array เป็นคีย์เวิรค์ เครื่องหมาย <> หมายถึงให้กำหนดชนิดของ
ข้อมูลในอาร์เรย์ ตัวแปรอาร์เรย์นี้เป็น reference จึงต้องมี handle (^)
นอกจากจะประกาศอาร์เรย์ตามข้างต้นแล้วยังสามารถสร้าง อาร์เรย์แบบธรรมดาได้ด้วยดังนี้
array_type array_name[size];
เช่น
int i[3];
ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
int main(){
int i[3]={3,4,5};
Console::WriteLine(i[0]+" "+i[1]+" "+i[2]);
Console::ReadLine();
return 0;
}
ผลลัพธ์ จะได้ 3 4 5
การอ้างอิงถึงสมาชิกของ อาร์เรย์สามารถใช้ เครื่องหมาย [ ตำแหน่งสมาชิกอาร์เรย์] ได้ดังนี้
i[0]=10; // หมายถึงกำหนดสมาชิกตัวแรก(ตำแหน่งแรก) ของอาร์เรย์ i ให้เป็น 10
การสร้างอาร์เรย์แบบ Reference Type
ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
int main(){
array<int>^ i =gcnew array<int>(3);
i[0]=10;
i[1]=20;
i[2]=30;
Console::WriteLine(i[0]+" "+i[1]+" "+i[2]);
Console::ReadLine();
return 0;
}
ผลลัพธ์จะได้ 10 20 30
ในตัวอย่างนี้จะสร้างอาร์เรย์ชนิด reference ซึ่งมีรูปแบบดังนี้
Code:array<int>^ i =gcnew array<int>(3);
สร้างตัวแปรอาร์เรย์ชื่อ i มีชนิดเป็น int โดยมีขนาดเป็น 3 สังเกตว่าในการสร้าง Reference type จะใช้ gcnew แทน new
นอกจากนั้นเราสามารถสร้างอาร์เรย์โดยกำหนดสมาชิกเข้าไปเลยก็ได้ดังนี้
Code:array<int>^ a =gcnew array<int>(3){100,200,300};
ในตัวอย่างนี้เป็นการสร้างตัวแปร a ที่มีชนิดอาร์เรย์ เป็นชนิด int มีสมาชิก 3 ตัวคือ 100,200,300
ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
int main(){
array<int>^ a =gcnew array<int>(3){100,200,300};
for(int i=0;i<a->Length;i++){
Console::WriteLine(a[i]+"\n");
}
Console::ReadLine();
return 0;
}
ผลลัพธ์จะได้
100
200
300
การใช้งาน Jagged arrayJagged array เป็นอาร์เรย์ซ้อนอาร์เรย์มีรูปแบบดังนี้
array<array<array_type>^>^ array_name =gcnew array<array<array_type>^>(array_size);
เช่น
Code:array<array<int>^>^ i =gcnew array<array<int>^>(3);
ตัวอย่างการใช้งาน Jagged Array
Code:#include "stdafx.h"
using namespace System;
int main(){
array<array<int>^>^ i =gcnew array<array<int>^>(3);// กำหนด Jagged Array เพื่อเก็บอาร์เรย์อีกที
i[0] = gcnew array<int>(1){9};//กำหนดให้สมาชิกอาร์เรย์ตัวแรกมีขนาด size เป็น 1 โดยเก็บเลข 9 ไว้
i[1] = gcnew array<int>(2){9,9};//กำหนดให้สมาชิกอาร์เรย์ตัวแรกมีขนาด size เป็น 2 โดยเก็บเลข 9,9 ไว้
i[2] = gcnew array<int>(3){9,9,9};//กำหนดให้สมาชิกอาร์เรย์ตัวแรกมีขนาด size เป็น 3 โดยเก็บเลข 9,9,9 ไว้
for(int c =0;c<i->Length;c++){//วนลูปอาร์เรย์หลักก่อนซึ่งมีสมาชิก 3 ตัว
for(int d = 0;d<i[c]->Length;d++){//วนลูปอาร์เรย์ที่ซ้อนอาร์เรย์หลัก
Console::Write(i[c][d]);//แสดงค่า
}
Console::WriteLine();
}
Console::ReadLine();
return 0;
}
ผลลัพธ์แสดงดังรูปที่ 1
การใช้อาร์เรย์หลายมิติ
การสร้างอาร์เรย์หลายมิติมีรูปแบบดังนี้
array<type,dimension>^ array_name =gcnew array<type>(size);
เช่น
array<int,3>^ i =gcnew array<int,3>(2,2,2);
ในตัวอย่างนี้เป็นการกำหนด array 3 มิติ
Code:#include "stdafx.h"
using namespace System;
int main(){
array<String^,2>^ a =gcnew array<String^,2>(5,5);
array<String^,2>^ b =gcnew array<String^,2>(5,5);
for(int x =0;x<a->GetLength(0);x++){
for(int y=0;y<a->GetLength(1);y++)
a[x,y]="*";
}
for(int x=0;x<b->GetLength(0);x++){
for(int y=0;y<b->GetLength(1);y++){
Console::Write(a[x,y]+"\t");
}
Console::WriteLine();
}
Console::ReadLine();
return 0;
}
ผลลัพธ์แสดงดังรูปที่สอง
Escape Sequences ต่างๆ\? แสดง เครื่องหมาย ?
\' แสดงเครื่องหมาย single quote(')
\" แสดงเครื่องหมาย Double Quote(")
\n ขึ้นบรรทัดใหม่
\a แสดงเสียง
\0 ค่า Null
\b Backspace
\r Carriage return
\t Tab
Literals ต่างๆได้แก่
Number Literals มี 5 ชนิดคือ Integer,Octal,Hexadecimal,Decimal,Exponential
Integer เป็นจำนวนเต็มเช่น 11,12,-11,-12
Octal เป็นเลขฐาน 8 ซึ่งมีค่าตั้งแต่ 0 ถึง 7 โดย ต้องขึ้นด้วยเลข 0 เสมอ เช่น 051,099
Hexadecimal เป็นเลขฐาน 16 ซึ่งมีค่าตั้งแต่ 0-9,A-F โดยจะต้องขึ้นต้นด้วย 0x เสมอเช่น 0x1234, 0x5567
Decimal เป็นเลขฐาน 10 ที่เราใช้กันทั้วไป
Exponential เป็นเลขยกกำลังเช่น 4.5e2
ตัวอย่าง Number Literal
Code:#include "stdafx.h"
using namespace System;
int main(){
Console::WriteLine(100);
Console::WriteLine("Octal "+043);
Console::WriteLine("Octal "+064);
Console::WriteLine("Hexadecimal "+0xAABBCC);
Console::WriteLine("Hexadecimal "+0x005566);
Console::WriteLine("Exponential "+4.3e10);
Console::ReadLine();
return 0;
}
String Literal เป็นค่าคงที่ข้อความต่างๆเช่น Hello,Welcome
ในการสร้าง String Literal นั้นจะต้องใช้ Handle(^) ด้วยเนื่องจากว่า String เป็น Reference type
ตัวอย่าง
Code:String^ s1 ="Hello";
String^ s2 =gcnew String("Welcome");
ตัวอย่างโค้ด
Code:#include "stdafx.h"
using namespace System;
int main(){
String^ s1 ="Hello";//สร้าง String Literal
String^ s2 =gcnew String("Welcome");//สร้าง String โดยใช้ gcnew
Console::WriteLine(s1);
Console::WriteLine(s2);
Console::ReadLine();
return 0;
}
ผลลัพธ์จะได้
Hello
Welcome
การใช้ Boolean Literal boolean literal เป็นค่าคงที่ที่บอกได้ว่าจริงหรือเท็จ
เช่น
bool b1 =false;//มีค่าความจริงเป็นเท็จ
bool b2 =true;//มีค่าความจริงเป็นจริง
ตัวอย่าง
Code:#include "stdafx.h"
using namespace System;
int main(){
bool b1 =true;
bool b2 =false;
bool b3 =3<5; //ตรวจสอบก่อนว่า 3 น้อยกว่า 5 เป็นจริงไหมผลลัพธ์จะได้ true
bool b4 =10 > 20; //ตรวจสอบก่อนว่า 10 > 20 เป็นจริงไหมผลลัพธ์จะได้ false
Console::WriteLine(b1);
Console::WriteLine(b2);
Console::WriteLine(b3);
Console::WriteLine(b4);
Console::ReadLine();
return 0;
}
ผลลัพธ์จะได้
True
False
True
False
paedotnet attached the following image(s):


[With great power comes great responsibility]