SlideShare una empresa de Scribd logo
1 de 18
การจัด การชุด ข้อ มูล โดย
ใช้อ าเรย์ (Array)
C# Programming
อาเรย์ค ือ อะไร
“อาเรย์เ ป็น ชุด ของข้อ มูล ซึ่ง มี
ประเภทข้อ มูล เดีย วกัน ”

Disk Array

Processor Array
ตัว อย่า ง: คลาส Score


พิจารณาโปรแกรมซึ่งรับ และเก็บ
ข้อมูลคะแนนในตัวแปร 3 ตัวแปรต่าง
กัน
using System;
using System;

class Scoring {
class Scoring {
static void Main() {
static void Main() {
int s1, s2, s3;
int s1, s2, s3;
double avg;
double avg;
Console.Write("Enter score #1: ");
Console.Write("Enter score #1: ");
s1 = int.Parse(Console.ReadLine());
s1 = int.Parse(Console.ReadLine());
Console.Write("Enter score #2: ");
Console.Write("Enter score #2: ");
s2 = int.Parse(Console.ReadLine());
s2 = int.Parse(Console.ReadLine());
Console.Write("Enter score #3: ");
Console.Write("Enter score #3: ");
s3 = int.Parse(Console.ReadLine());
s3 = int.Parse(Console.ReadLine());

}
}

}
}

avg = (s1+s2+s3)/3.0;
avg = (s1+s2+s3)/3.0;
Console.WriteLine("Average score is {0:f2}.", avg);
Console.WriteLine("Average score is {0:f2}.", avg);
หากจำา นวนนัก เรีย นมาก
ขึ้น ? ่จะมีนักเรียนเพียงสองคน กลับมี
 แทนที
นักเรียนจำานวน 60 คน
using System;
using System;
class Scoring {{
class Scoring
static void Main() {{
static void Main()
int s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, ???;
int s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, ???;
double avg;
double avg;
Console.Write("Enter score #1: ");
Console.Write("Enter score #1: ");
s1 == int.Parse(Console.ReadLine());
s1
int.Parse(Console.ReadLine());
Console.Write("Enter score #2: ");
Console.Write("Enter score #2: ");
s2 == int.Parse(Console.ReadLine());
s2
int.Parse(Console.ReadLine());
Console.Write("Enter score #3: ");
Console.Write("Enter score #3: ");
s3 == int.Parse(Console.ReadLine());
s3
int.Parse(Console.ReadLine());
Console.Write("Enter score #4: ");
Console.Write("Enter score #4: ");
s4 == int.Parse(Console.ReadLine());
s4
int.Parse(Console.ReadLine());
Console.Write("Enter score #5: ");
Console.Write("Enter score #5: ");
s5 == int.Parse(Console.ReadLine());
s5
int.Parse(Console.ReadLine());
Console.Write("Enter score #6: ");
Console.Write("Enter score #6: ");
s6 == int.Parse(Console.ReadLine());
s6
int.Parse(Console.ReadLine());
::
::
}}
}}

อีก ทางเลือ กหนึ่ง :
ใช้อ าเรย์
อาเรย์ใ น C#
อาเรย์เ ป็น โครงสร้า งข้อ มูล ซึ่ง เก็บ
ชุด ข้อ มูล ที่ม ีป ระเภทข้อ มูล เหมือ น
กัน
 เทอมในอาเรย์
Indices

First index

0 1 2 3 4 5 6 7 8 9

Array

Element (at index 7)
Length = 10



อาเรย์สามารถเข้าถึงได้ผ่าน array
variable s
มิต ิใ นอาเรย์


อาเรย์หนึ่งมิติ

0 1 2 3 4 5 6 7 8 9 10 11



อาเรย์หลายมิติ


ตัวอย่าง อาเรย์สองมิติ
3
2
1
0
0 1 2 3 4 5 6 7 8
การประกาศตัว แปรอาเรย์



ตัวแปรอาเรย์ใช้สำาหรับอ้างถึงอาเรย์
ไวยากรณ์:
<type>[] <name>;
<type>[] <name>;



ตัวอย่าง:
// "scores" refers to an array of integers
// "scores" refers to an array of integers
int[] scores;
int[] scores;
// "grades" refers to an array of characters
// "grades" refers to an array of characters
char[] grades;
char[] grades;
// "names" refers to an array of strings
// "names" refers to an array of strings
string[] names;
string[] names;
การสร้า งอาเรย์


อาเรย์จริงจะถูกสร้างโดยใช้ตัวดำาเนินการ
new




new <type>[<#elements>]

ไวยากรณ์ข้างต้นถือเป็นนิพจน์

อาเรย์ซงสร้างขึ้นมาใหม่สามารถกำาหนด
ึ่
ให้กับตัวแปรอาเรย์สำาหรับการอ้างอิงต่อ
int[] scores;
ไปได้ใint[] scores;
นอนาคต


ตัวอย่าง:

scores = new int[10];
scores = new int[10];

int[] scores = new int[10];
int[] scores = new int[10];



หรือ
การประกาศและกระบวนการ
สร้า งอาเรย์
int[] scores;
int[] scores;
scores = new int[10];
scores = new int[10];

scores

??

หน่ว ยความจำา คอมพิว เตอร์
การสร้า งอาเรย์ด ว ยการ
้
กำา หนดค่า เริ่ม ต้น



อาเรย์อาจจะถูกสร้างโดยใช้คาเริ่มต้น
่
ไวยากรณ์ #1:
new <type>[<#elements>] {value1,value2,...}



ตัวอย่าง:

int[] scores = new int[4] {30,20,50,25};
int[] scores = new int[4] {30,20,50,25};



ไวยากรณ์ #2:
{value1,value2,...}



ตัวอย่าง:
int[] scores = {30,20,50,25};
int[] scores = {30,20,50,25};
การเข้า ถึง ข้อ มูล ในอาเรย์


ไวยากรณ์:
<name>[<index>]


<index> เป็นจำานวนเต็มตั้งแต่ 0 และขนาด

ของอาเรย์ - 1



ตัวอย่าง

int[] scores = new int[5];
int[] scores = new int[5];
scores[0] = 15;
scores[0] = 15;
scores[3] = 52;
scores[3] = 52;
Console.WriteLine(scores[3]);
Console.WriteLine(scores[3]);
Console.WriteLine(scores[2]);
Console.WriteLine(scores[2]);

0 1
15 0
0

2 3 4
0 52 0
0

scores
ตัว อย่า ง: Class Score
using System;
using System;
class Scoring {
class Scoring {
static void Main() {
static void Main() {
int[] s = new int[60];
int[] s = new int[60];
int i;
int i;
double avg;
double avg;

}
}

}
}

for (i = 0; i < 60; i++) {
for (i = 0; i < 60; i++) {
Console.Write("Enter score #{0}: ", i+1);
Console.Write("Enter score #{0}: ", i+1);
s[i] = int.Parse(Console.ReadLine());
s[i] = int.Parse(Console.ReadLine());
}
}
:
:
:
:
คำาสั่ง foreach



ใช้สำาหรับการวนรอบข้อมูลทุกตัวทีอยู่
่
ในอาเรย์
foreach :
ไวยากรณ์(<type> <var> in <array>)
statement;



ตัวint[]าa : new int[] {1,2,3,4,5};
อย่ ง =
int[] a = new int[] {1,2,3,4,5};
int sum = 0;
int sum = 0;
foreach (int i in a)
foreach (int i in a)
{
{
sum = sum + i;
sum = sum + i;
}
}
การส่ง อาเรย์ไ ปยัง เมท็อ ด


เมท็อดต่อไปนี้ทำาการหาผลรวมของ
ทุกค่าข้อมูลทีอยู่ในอาเรย์
่
static double Sum(double[] array) {
static double Sum(double[] array) {
double sum = 0.0;
double sum = 0.0;
foreach (double x in array)
foreach (double x in array)
sum += x;
sum += x;
}
}

return sum;
return sum;
การตรวจสอบขนาดขอ
งอาเรย์

By property Le ng th

int[] scores = new int[5];
int[] scores = new int[5];
Console.WriteLine(scores.Length);
Console.WriteLine(scores.Length);



By method Ge tLe ng th
int[] scores = new int[5];
int[] scores = new int[5];
Console.WriteLine(scores.GetLength(0));
Console.WriteLine(scores.GetLength(0));
ตัว อย่า งการหาขนาดขอ
งอาเรย์


อีกวิธีหนึงสำาหรับการหาผลรวมของ
่
ค่าข้อมูลในอาเรย์
double Summation(double[] array) {
double Summation(double[] array) {
double sum = 0.0;
double sum = 0.0;
for (int i = 0; i < array.Length; i++)
for (int i = 0; i < array.Length; i++)
sum += array[i];
sum += array[i];
}
}

return sum;
return sum;
String คือ อาเรย์ข อง
อักวอย่าง
ขระ
 ตั
string s = "Hello";
string s = "Hello";
Console.WriteLine("Length = {0}", s.Length);
Console.WriteLine("Length = {0}", s.Length);
Console.WriteLine("First char is {0}", s[0]);
Console.WriteLine("First char is {0}", s[0]);
Console.WriteLine("Last char is {0}", s[s.Length-1]);
Console.WriteLine("Last char is {0}", s[s.Length-1]);

0

1

2

3

4

'H' 'e' 'l' 'l' 'o'

s
ตัว อย่า งการหาค่า สูง สุด


เขียนเมท็อด FindM เพือคำานวนหา
ax ่
ค่าข้อมูลทีสงทีสดในอาเรย์ชนิด
่ ู ่ ุ
doubles
static double FindMax(double[] data) {
static double FindMax(double[] data) {
double max = data[0];
double max = data[0];
foreach (double x in data)
foreach (double x in data)
if (x > max) max = x;
if (x > max) max = x;

}
}

return max;
return max;

Más contenido relacionado

La actualidad más candente

หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา ปาโจด ม.5
หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา  ปาโจด ม.5หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา  ปาโจด ม.5
หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา ปาโจด ม.5สิรินยา ปาโจด
 
หน่วยที่2 โครงสร้างข้อมูล นาย ธนพงษ์ น่านกร เลขที่ 1 ชั้น ม.5
หน่วยที่2 โครงสร้างข้อมูล  นาย ธนพงษ์  น่านกร  เลขที่ 1   ชั้น ม.5หน่วยที่2 โครงสร้างข้อมูล  นาย ธนพงษ์  น่านกร  เลขที่ 1   ชั้น ม.5
หน่วยที่2 โครงสร้างข้อมูล นาย ธนพงษ์ น่านกร เลขที่ 1 ชั้น ม.5palmyZommanow
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์Theeravaj Tum
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริงบทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริงdefeat overcome
 
Java Programming: คลาสอินพุตและเอาต์พุต
Java Programming: คลาสอินพุตและเอาต์พุตJava Programming: คลาสอินพุตและเอาต์พุต
Java Programming: คลาสอินพุตและเอาต์พุตThanachart Numnonda
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1Mook Prapasson
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1prapassonmook
 

La actualidad más candente (20)

Array1
Array1Array1
Array1
 
หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา ปาโจด ม.5
หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา  ปาโจด ม.5หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา  ปาโจด ม.5
หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา ปาโจด ม.5
 
หน่วยที่2 โครงสร้างข้อมูล นาย ธนพงษ์ น่านกร เลขที่ 1 ชั้น ม.5
หน่วยที่2 โครงสร้างข้อมูล  นาย ธนพงษ์  น่านกร  เลขที่ 1   ชั้น ม.5หน่วยที่2 โครงสร้างข้อมูล  นาย ธนพงษ์  น่านกร  เลขที่ 1   ชั้น ม.5
หน่วยที่2 โครงสร้างข้อมูล นาย ธนพงษ์ น่านกร เลขที่ 1 ชั้น ม.5
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
03 input math
03 input math03 input math
03 input math
 
Chapter2
Chapter2Chapter2
Chapter2
 
PHP Tutorial (array)
PHP Tutorial (array)PHP Tutorial (array)
PHP Tutorial (array)
 
บทที่ 5
บทที่ 5บทที่ 5
บทที่ 5
 
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริงบทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง
 
Java Programming: คลาสอินพุตและเอาต์พุต
Java Programming: คลาสอินพุตและเอาต์พุตJava Programming: คลาสอินพุตและเอาต์พุต
Java Programming: คลาสอินพุตและเอาต์พุต
 
งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 
งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8
 
Inet
InetInet
Inet
 
4
44
4
 
New presentation1
New presentation1New presentation1
New presentation1
 
Computer Programming 3
Computer Programming 3 Computer Programming 3
Computer Programming 3
 
C lang
C langC lang
C lang
 

Similar a 08 arrays

คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
ตัวแปรชุด
ตัวแปรชุดตัวแปรชุด
ตัวแปรชุดPear Pimnipa
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysWongyos Keardsri
 
09 multi arrays
09 multi arrays09 multi arrays
09 multi arraysa-num Sara
 
ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระAreeya Onnom
 
ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระAreeya Onnom
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระบทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระMook Sasivimon
 
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptxRTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptxnkrafacyberclub
 
ตัวแปรชุดนำเสนอ
ตัวแปรชุดนำเสนอตัวแปรชุดนำเสนอ
ตัวแปรชุดนำเสนอPz'Peem Kanyakamon
 
ตัวแปรชุดและตัวแปรอักขระ PPT
ตัวแปรชุดและตัวแปรอักขระ PPTตัวแปรชุดและตัวแปรอักขระ PPT
ตัวแปรชุดและตัวแปรอักขระ PPTAreeya Onnom
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระบทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระMook Sasivimon
 
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระบทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระMook Sasivimon
 

Similar a 08 arrays (20)

คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
02 basic
02 basic02 basic
02 basic
 
งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8
 
ตัวแปรชุด
ตัวแปรชุดตัวแปรชุด
ตัวแปรชุด
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional Arrays
 
09 multi arrays
09 multi arrays09 multi arrays
09 multi arrays
 
ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระ
 
ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระ
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระบทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
 
ข้อมูลชนิดอาร์เรย์และสตริง
ข้อมูลชนิดอาร์เรย์และสตริงข้อมูลชนิดอาร์เรย์และสตริง
ข้อมูลชนิดอาร์เรย์และสตริง
 
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptxRTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
RTAF_Basic_Python_2022_Cyber_Operation_Contest.pptx
 
ข้อมูลชนิด Array
ข้อมูลชนิด Arrayข้อมูลชนิด Array
ข้อมูลชนิด Array
 
ข้อมูลชนิด Array
ข้อมูลชนิด Arrayข้อมูลชนิด Array
ข้อมูลชนิด Array
 
ตัวแปรชุดนำเสนอ
ตัวแปรชุดนำเสนอตัวแปรชุดนำเสนอ
ตัวแปรชุดนำเสนอ
 
Java-Answer Chapter 05-06
Java-Answer Chapter 05-06Java-Answer Chapter 05-06
Java-Answer Chapter 05-06
 
ตัวแปรชุดและตัวแปรอักขระ PPT
ตัวแปรชุดและตัวแปรอักขระ PPTตัวแปรชุดและตัวแปรอักขระ PPT
ตัวแปรชุดและตัวแปรอักขระ PPT
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระบทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
 
Know1 3
Know1 3Know1 3
Know1 3
 
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระบทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
 

Más de a-num Sara

Más de a-num Sara (10)

Webprogramming
WebprogrammingWebprogramming
Webprogramming
 
C#
C#C#
C#
 
10 win apps
10 win apps10 win apps
10 win apps
 
07 methods
07 methods07 methods
07 methods
 
06 for loops
06 for loops06 for loops
06 for loops
 
05 loops
05 loops05 loops
05 loops
 
04 conditional
04 conditional04 conditional
04 conditional
 
02 basic
02 basic02 basic
02 basic
 
01 intro
01 intro01 intro
01 intro
 
C sharp ide
C sharp ideC sharp ide
C sharp ide
 

08 arrays