การใช้งาน Array
Array เป็นกลุ่มของข้อมูลชนิดเดียวกันเช่น กลุ่มของข้อมูลชนิด จำนวนเต็ม เช่น 1,3,5,6,8,4,3
การสร้าง Array
type_array[] array_name;
เช่น
int[] a;
เราสามารถกำหนดค่าเริ่มต้นให้กับarray ได้ดังนี้
int[] a ={1,2,3,4,5};
ในตัวอย่างนี้สร้างอาร์เรย์ชื่อว่า a เก็บสมาชิกทั้งหมด 5 ตัว
การอ้างอิงถึงสมาชิกอาร์เรย์
ตำแหน่งสมาชิกตัวแรกคือ 0,ตัวที่สอง คือ 1,....
เช่น
int[] a={1,2,3,4,5};
ถ้าเราต้องการอ้างอิงถึงสมาชิกเลข 1 เราก็เขียน a[0] ถ้าเราต้องการอ้างอิงถึงเลข 4 เราก็เขียน a[3]
ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int[] a = { 11, 22, 33, 44, 55 };
Console.WriteLine("member 1 = "+a[0]+" member 2 = "+a[1]+" member 3 = "+a[2]);
Console.ReadLine();
}
}
}
ผลลัพธ์จะได้
member 1 =11 member 2 =22 member 3 =33
การสร้างอาร์เรย์โดยใช้ new
array_type[] array_name = new array_type[size];
เช่น
int[] a =new int[3];
ถ้าเราต้องการสร้างอาร์เรย์แล้วกำหนดสมาชิกเข้าไปเลยก็เขียนได้ดังนี้
int[] a =new int[3]{1,2,3};
หรือ
int[] a =new int[]{1,2,3,4,5}; ->แบบนี้จะต่างจากแบบแรกตรงที่สามารถกำหนดสมาชิกได้ไม่จำกัด
ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[3];
for (int i = 0; i < 3; i++)//วนลูปเพื่อกำหนดค่าให้กับสมาชิก
{
a[i] = i * i;
}
for (int i = 0; i < 3; i++)//วนลูปเพื่อแสดงค่าของสมาชิก
{
Console.WriteLine("Show : " + a[i]);
}
Console.ReadLine();
}
}
}
ผลลัพธ์จะได้
Show : 0
Show : 1
Show : 4
การใช้งาน properties Length
Length ใช้หาขนาดของสมาชิกอาร์เรย์
ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int[] a = { 56, 43, 75, 32, 88, 32 };
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i] + "\n");
}
Console.ReadLine();
}
}
}
ผลลัพธ์จะได้
56
43
75
32
88
32
การใช้เมธอด Sort()
Sort() เป็นการจัดเรียงอาร์เรย์จากน้อยไปมาก
ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int[] a = { 56, 43, 75, 32, 88, 32 };
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i] + "\n");
}
Console.WriteLine("After Sort");
Array.Sort(a);
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i] + "\n");
}
Console.ReadLine();
}
}
}
ผลลัพธ์ลัพธ์สมาชิกอาร์เรย์จะเรียงจากน้อยไปมาก
การใช้ GetValue()
GetValue(int index) เป็นการหาสมาชิกในตำแหน่งที่ index
ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int[] a = { 56, 43, 75, 32, 88, 32 };
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i] + "\n");
}
Console.WriteLine("Find index = 3 : "+a.GetValue(3));
Console.ReadLine();
}
}
}
การใช้อาร์เรย์กับ string
รูปแบบ
string[] str =new string[3];
ตัวอย่าง
using System;
class Program
{
static void Main(string[] args)
{
string[] str = new string[3];//สร้างอาร์เรย์ชนิด string มีสมาชิกเป็น 3
str[0] = "A";//กำหนดสมาชิกให้กับอาร์เรย์
str[1] = "B";
str[2] = "C";
foreach (string s in str)//วนลูปเพื่อแสดงสมาชิกแต่ละตัว
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
ผลลัพธ์
A
B
C
การสร้างอาร์เรย์โดยใช้ชนิดเป็น object
ถ้าเราสร้างอาร์เรย์แบบนี้เราสามารถกำหนดให้สมาชิกเป็น int,string ฯ ก็ได้ เช่น
ตัวอย่าง
using System;
class TestArray
{
public static void Main()
{
object[] o = new object[5];
for (int i = 0; i < 3; i++)
{
o[i] = i * i;
}
o[3] = "Hello";
o[4] = "How are you";
for (int i = 0; i < o.Length; i++)
{
Console.WriteLine(o[i] + "\n");
}
Console.ReadLine();
}
}
การ copy อาร์เรย์
รูปแบบ Array.Copy(Array sourcearray,Array destination,int length)
ตัวอย่าง
using System;
public class TestArray
{
public static void Main()
{
int[] a = { 99, 88, 66 };
int[] b=new int[a.Length];
Array.Copy(a, b, a.Length);//copy สมาชิกของอาร์เรย์ a ไปที่ b
for (int i = 0; i <a.Length; i++)
{
Console.Write(b[i]+"\t");
}
Console.ReadLine();
}
}
การใช้เมธอด BinarySearch
BinarySearch ใช้ในการหาสมาชิกตามที่ index ระบุรูปแบบ
Array.BinarySearch(Array a,object value)
โดย a คือ อาร์เรย์ที่ต้องการ หา
value คือสมาชิกที่ต้องการหาตำแหน่ง
ตัวอย่าง
using System;
public class TestArray
{
public static void Main()
{
int[] a = { 6, 8, 4 };
Array.Sort(a);//จัดเรียงสมาชิกก่อน จะได้(4,6,8)
int i = Array.BinarySearch(a,8);
Console.WriteLine("index is " + i);
Console.ReadLine();
}
}
การใช้งาน อาร์เรย์หลายมิติ
รูปแบบ
อาร์เรย์ สองมิติ
array_type[,] array_name = new array_type[size,size];
อาร์เรย์สามมิติ
array_type[,,] array_name =new array_type[size,size,size];
ตัวอย่าง
using System;
public class TestArray
{
public static void Main()
{
int[,] a = new int[2, 2];
a[0, 0] = 1;//กำหนดสมาชิกแถวแรกและคอลัมภ์แรก
a[0, 1] = 2;//กำหนดสมาชิกแถวแรกและคอลัมภ์สอง
a[1, 0] = 3;//กำหนดสมาชิกแถวสองและคอลัมภ์แรก
a[1, 1] = 4;//กำหนดสมาชิกแถวสองและคอลัมภ์สอง
for (int i = 0; i < 2; i++)//วนแถว
{
for (int j = 0; j < 2; j++)//วนคอลัมภ์
{
Console.Write(a[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
ผลลัพธ์
12
34
หรืออีกแบบเราสามารถกำหนดค่าเริ่มต้นของสมาชิกได้เลยดังนี้
using System;
public class TestArray
{
public static void Main()
{
int[,] a = { { 1, 2 }, { 3, 4 } };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(a[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
การใช้งานอาร์เรย์สามมิติ
ตัวอย่าง
using System;
public class TestArray
{
public static void Main()
{
int[, ,] a = new int[2, 2, 2];//สร้างอาร์เรย์ 3มิติขนาด 2x2x2
// กำหนดค่าเริ่มต้นให้กับสมาชิกก่อน
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
a[i, j, k] = i+j+k;
}
}
}
//แสดงค่า
for (int i = 0; i < 2; i++)
{
Console.WriteLine("Dimension :[" + i + "]");
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
Console.Write(a[i, j, k] + "\t");
}
Console.WriteLine();
}
}
Console.ReadLine();
}
}
ผลลัพธ์แสดงดังรูป

การใช้งาน Jagged array
jagged array เป็นการสร้างอาร์เรย์ซ้อนอาร์เรย์
ตัวอย่าง
using System;
public class TestArray
{
public static void Main()
{
int[][] a = new int[5][];//สร้าง jagged array
a[0] = new int[1]{9};//อาร์เรย์ซ้อนอาร์เรย์ตัวที่หนึ่ง
a[1] = new int[2]{9,9};
a[2] = new int[3]{9,9,9};
a[3] = new int[4]{9,9,9,9};
a[4] = new int[5]{9,9,9,9,9};
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < a[i].Length; j++)
{
Console.Write(a[i][j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
ผลลัพธ์แสดงดังรูป

การสร้าง array จาก คลาส Array
รูปแบบ
Array array_name =Array.CreateInstance(typeof(type),size);
เช่น
Array a =Array.CreateInstance(typeof(int),3);
ในตัวอย่างนี้หมายถึงสร้างอาร์เรย์ชื่อ a มีชนิดเป็น int และมีจำนวนสมาชิกเป็น 3
การกำหนดสมาชิกของ array ก็ใช้ SetValue รูปแบบ
object_of_array.SetValue(Object value,int index);
ตัวอย่าง
using System;
public class TestArray
{
public static void Main()
{
Array a = Array.CreateInstance(typeof(string),10);
for (int i = 0; i <a.Length; i++)
{
a.SetValue("*", i);
}
for (int i = 0; i <a.Length; i++)
{
Console.Write(a.GetValue(i)+"\t");
}
Console.ReadLine();
}
}
ผลลัพธ์จะได้ * * * * * * * * * *
ตัวอย่าง
using System;
using System.Collections;
public class TestArray
{
public static void Main()
{
Array a = Array.CreateInstance(typeof(string),10);
for (int i = 0; i <a.Length; i++)
{
a.SetValue("*", i);
}
IEnumerator ii = a.GetEnumerator();//เรียกใช้ interface IEnumerator
while (ii.MoveNext())//วนลูปแสดงตัวถัดไป
{
Console.WriteLine(ii.Current);//แสดงตัวปัจจุบัน
}
Console.ReadLine();
}
}


[With great power comes great responsibility]