อาเรย์เป็นประเภทข้อมูลชนิดหนึ่ง ที่คุณสามารถเก็บค่าตัวแปรประเภทเดียวกันได้หลายๆ ค่าไว้ในตัวแปรเดียวที่เรียกว่า ตัวแปรอาเรย์
อาเรย์ 1 มิติ
รูปแบบ
int[] array1 = new int[5]; // การประกาศตัวแปร array int[] array2 = new int[] { 1, 2, 3, 4, 5};
ตัวอย่าง
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace array { class Program { static void Main(string[] args) { int[] i = new int[] {1,2,3,4,5}; int[] j = new int[5]; float[] k = new float[] { 5.4f, 2.2f, 3.4f, 2.3f, 5.2f }; j[3] = 4; Console.WriteLine("I Index 2 :: {0} ",i[2]); Console.WriteLine("J Index 3 :: {0} ",j[3]); Console.WriteLine("K Index 4 :: {0} ",k[4]); Console.ReadLine(); } } }
ผลการรัน
I Index 2 :: 3
J Index 3 :: 4
K Index 4 :: 5.2
คำสั่ง For loop กับอาเรย์
using System; namespace array_for_loop { class Program { static void Main(string[] args) { int[] array = new int[5]; for (int i = 0; i < 5; i++) { array[i] = (i + 2); Console.Write(array[i] + " "); } Console.ReadLine(); } } }
ผลการรัน
2 3 4 5 6
อาเรย์ 2 มิติ
การสร้างอาเรย์สองมิตินั้นคล้ายกับอาเรย์หนึ่งมิติ แต่มันแค่ต้องการเครื่องหมาย , คั่นระหว่างมิติสำหรับการสร้าง
รูปแบบ
int[,] array1 = new int[2, 3]; int[,] array2 = new int[,] { {1, 2, 3}, {4, 5, 6}};
ตัวอย่าง
using System; namespace array_2miti { class Program { static void Main(string[] args) { int[,] array2 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; Console.WriteLine("[0,0] = " + array2[0, 0]); // read 1 Console.WriteLine("[0,1] = " + array2[0, 1]); // read 2 Console.WriteLine("[0,2] = " + array2[0, 2]); // read 3 Console.WriteLine("[1,0] = " + array2[1, 0]); // read 4 Console.WriteLine("[1,1] = " + array2[1, 1]); // read 5 Console.WriteLine("[1,2] = " + array2[1, 2]); // read 6 Console.ReadLine(); } } }
ผลการรัน
[0,0] = 1
[0,1] = 2
[0,2] = 3
[1,0] = 4
[1,1] = 5
[1,2] = 6
For loop กับอาเรย์สองมิติ
ตัวอย่าง
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace array_2miti_for_loop { class Program { static void Main(string[] args) { int[,] array = new int[,] { { 00,01, 02, 03, 04, 05 }, { 10, 11, 12, 13, 14, 15 }, { 20, 21, 22, 23, 24, 25 }, { 30, 31, 32, 33, 34, 35 } }; for (int i = 0; i < 4; i++) // for จำนวนกลุ่มของ array { for (int j = 0; j < 5; j++) // for จำนวนของข้อมูลในแต่ละกลุ่ม array { Console.WriteLine("Index [{0},{1}] = {2} ",i,j, array[i,j]); } } Console.ReadLine(); } } }
ผลการรัน
Index [0,0] = 0
Index [0,1] = 1
Index [0,2] = 2
Index [0,3] = 3
Index [0,4] = 4
Index [1,0] = 10
Index [1,1] = 11
Index [1,2] = 12
Index [1,3] = 13
Index [1,4] = 14
Index [2,0] = 20
Index [2,1] = 21
Index [2,2] = 22
Index [2,3] = 23
Index [2,4] = 24
Index [3,0] = 30
Index [3,1] = 31
Index [3,2] = 32
Index [3,3] = 33
Index [3,4] = 34
อาเรย์ของอาเรย์ (Jagged)
เก็บข้อมูลแบบอาเรย์ไว้ในอาเรย์ เช่นเดียวกันกับเก็บตัวแปรปกติไว้ในอารเย์ คล้าย ๆ กับอาเรย์หลายติมิแต่ไม่ทั้งหมด เรียกว่าอาเรย์ของอาเรย์ หรืออาเรย์ jagged
รูปแบบ
int[][] jaggedArray = new int[6][];
ตัวอย่าง
using System; namespace array_jagged { class Program { static void Main(string[] args) { int[][] array = new int[2][]; // ข้อมูลจะมี 2 แถว for (int i = 0; i < array.Length; i++) { array[i] = new int[] { i+1, i+2, i+3, i+4}; // ข้อมูลของแต่ละแถว มี 4 column /* จากคำสั่งข้างบนจะแสดงแบบปกติ แบบนี้ int[][] array = new int[][] { new int[] {1,2,3,4}, // แถวที่ 1 new int[] {2,3,4,5}, // แถวที่ 2 } */ } for (int i = 0; i < array.Length; i++) { for (int j = 0; j < array[i].Length; j++) // array[i] มีข้อมูล 4 column { Console.WriteLine("array [{0},{1}] = {2}", i, j, array[i][j]); } } Console.ReadLine(); } } }
ผลการรัน
array [0,0] = 1
array [0,1] = 2
array [0,2] = 3
array [0,3] = 4
array [1,0] = 2
array [1,1] = 3
array [1,2] = 4
array [1,3] = 5
// Sort an intint[] myNumbers = {5, 1, 8, 9}; Array.Sort(myNumbers);foreach (int i in myNumbers){ Console.WriteLine(i);}
int[] myNumbers = {5, 1, 8, 9}; Console.WriteLine(myNumbers.Max()); // returns the largest value Console.WriteLine(myNumbers.Min()); // returns the smallest value Console.WriteLine(myNumbers.Sum()); // returns the sum of elements