More Related Content
Java-Chapter 10 Two Dimensional Arrays Java-Chapter 07 One Dimensional Arrays บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1 What's hot (19)
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง Java-Chapter 02 Data Operations and Processing ตัวแปรชุดและตัวแปรอักขระ PPT Java-Chapter 05 String Operations บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง Java-Answer Chapter 07 (For Print) Java-Answer Chapter 10-11 (For Print) Java-Answer Chapter 08-09 (For Print) Java-Answer Chapter 08-09 Java-Answer Chapter 10-11 Similar to 09 multi arrays (20)
Java Programming [8/12] : Arrays and Collection Java Programming: อะเรย์และคอลเล็กชั่น บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w] บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขร (Big One) C Language - 01 ฟังก์ชันส่งผ่านอาร์เรย์ ข้อมูลชนิดอาร์เรย์และสตริง More from a-num Sara (10)
09 multi arrays
- 2. มิต ิข องอาเรย์
อาเรย์หนึ่งมิติ
0 1 2 3 4 5 6 7 8 9 10 11
ระบุจ ำา นวนเต็ม
หนึ่ง ค่า สำา หรับ
เป็น ดัช นี
อาเรย์สองมิติ
0
1
2
3
A[7]
ระบุจ ำา นวนเต็ม
สองค่า สำา หรับ
เป็น ดัช นี
A[1,4]
0 1 2 3 4 5 6 7 8
- 3. การประกาศอาเรย์ห ลาย
มิต ิ
สองมิติ
<type>[,] <name>;
<type>[,] <name>;
ตัวอย่าง
int[,] Array2D;
int[,] Array2D;
สามมิติ
<type>[,,] <name>;
<type>[,,] <name>;
ตัวอย่าง
int[,,] Array3D;
int[,,] Array3D;
- 4. การสร้า งอาเรย์ห ลายมิต ิ
สองมิติ
<name> = new <type>[<dim1>,<dim2>];
<name> = new <type>[<dim1>,<dim2>];
ตัวอย่าง
int[,] Array2D;
int[,] Array2D;
Array2D = new int[3,5];
Array2D = new int[3,5];
สามมิติ
<name> = new <type>[<dim1>,<dim2>,<dim3>];
<name> = new <type>[<dim1>,<dim2>,<dim3>];
ตัวอย่าง
int[,,]
int[,,]
Array3D
Array3D
Array3D;
Array3D;
= new int[3,5,2];
= new int[3,5,2];
- 5. ตัว อย่า ง
int[,] Array2D;
int[,] Array2D;
Array2D = new int[3,5];
Array2D = new int[3,5];
int[,] Array3D;
int[,] Array3D;
Array3D = new int[4,3,5];
Array3D = new int[4,3,5];
- 6. การกำา หนดค่า เริ่ม ต้น
แบบที่หนึ่ง เช่น
int[,] Array2D;
int[,] Array2D;
Array2D = new int[2,3] {{5,4,3},{2,1,0}};
Array2D = new int[2,3] {{5,4,3},{2,1,0}};
แบบที่สอง เช่น
int[,] Array2D;
int[,] Array2D;
Array2D = new int[,] {
Array2D = new int[,] {
{5,4,3},
{5,4,3},
{2,1,0}
{2,1,0}
};
};
แบบที่สามอย่างย่อ เช่น
int[,] Array2D;
int[,] Array2D;
Array2D = {
Array2D = {
{5,4,3},
{5,4,3},
{2,1,0}
{2,1,0}
};
};
5
2
4
1
3
0
- 7. การเข้า ถึง ข้อ มูล ในอาเรย์
จำานวนตัวเลขที่ใช้เป็นดัชนีขึ้นอยู่กับ
จำานวนมิตของอาเรย์
ิ
int[,] A = {
int[,] A = {
{51,24,3},
{51,24,3},
{72,11,90},
{72,11,90},
{42,41,67},
{42,41,67},
{37,1,0}
{37,1,0}
};
};
0
1
2
3
A[2,0]
51 24 3
72 11 90
42 41 67
37 1 0
0 1 2
A[1,1]
A[3,2]
- 8. ขนาดของอาเรย์
ทบทวน
Le ng th property
Ge tLe ng th method
GetLength(1)
5
2
4
1
3
0
GetLength(0)
ตัวอย่าง
int[,] Array2D = {
int[,] Array2D = {
{5,4,3},
{5,4,3},
{2,1,0}
{2,1,0}
};
};
Console.WriteLine(Array2D.GetLength(0));
Console.WriteLine(Array2D.GetLength(0));
Console.WriteLine(Array2D.GetLength(1));
Console.WriteLine(Array2D.GetLength(1));
Console.WriteLine(Array2D.Length);
Console.WriteLine(Array2D.Length);
Length
- 9. แบบฝึก หัด
พิจารณาอาเรย์
int[,] A = {
int[,] A = {
{5,34,3,1},
{5,34,3,1},
{27,11,90,9},
{27,11,90,9},
{24,41,67,6},
{24,41,67,6},
{73,1,0,4},
{73,1,0,4},
{1,2,3,4}
{1,2,3,4}
};
};
จงหาค่าของนิพจน์
ต่อไปนี
A[2,2]้
A[3,0]
A[1,3]
A.Length
A.GetLength(0)
A.GetLength(1)
- 10. แบบฝึก หัด
พิจารณาอาเรย์
int[,] B = {
int[,] B = {
{5,2,3,1,0},
{5,2,3,1,0},
{11,9,10,8,2},
{11,9,10,8,2},
{-1,20,4,33,12},
{-1,20,4,33,12},
{10,11,8,9,7}
{10,11,8,9,7}
};
};
จงหาค่าของนิพจน์
ต่อไปนี
B[2,2]้
B[3,0]
B[1,3]
B.Length
B.GetLength(0)
B.GetLength(1)
- 11. การประยุก ต์ใ ช้อ าเรย์ส อง
มิตาหนดให้ Aก ซ์Bเป็นเมตริกซ์ขนาด
กำ ิ: เมตริ และ
3x3
1 3 2
A = 9 12 0
5 4 6
4 1 3
B = 5 2 6
6 3 9
using System;
using System;
class Matrix {{
class Matrix
public static void Main() {{
public static void Main()
int [,] AA ==
1
int [,]
คำานวน A B
+
int [,] BB ==
int [,]
2
int i,j;
int i,j;
}}
}}
for (i == 0; ii << 3; i++) {{
for (i
0;
3; i++)
for (j == 0; jj << 3; j++) {{
for (j
0;
3; j++)
Console.Write("{0,4}", A[i,j]+B[i,j]);
Console.Write("{0,4}", A[i,j]+B[i,j]);
}}
Console.WriteLine();
Console.WriteLine();
}}
- 12. เมท็อ ดที่ส ำา คัญ สำา หรับ เม
ตริก ซ์
ReadMatrix()
ShowMatrix()
- 13. เมท็อ ด ReadMatrix()
รับค่าพารามิเตอร์สองค่าคือ
จำานวนแถว, nr
จำานวนหลัก, nc
ทำาหน้าที่อ่านค่าจำานวนเต็ม nr×nc ค่าจากผูใช้
้
คืนค่าเป็นอาเรย์สองมิติซึ่งแทนเมตริกซ์
static int[,] ReadMatrix(int nr, int nc) {
static int[,] ReadMatrix(int nr, int nc) {
int[,] m = new int[nr,nc];
int[,] m = new int[nr,nc];
for (int i = 0; i < nr; i++) {
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
for (int j = 0; j < nc; j++) {
Console.Write("Element [{0},{1}]: ", i+1, j+1);
Console.Write("Element [{0},{1}]: ", i+1, j+1);
m[i,j] = int.Parse(Console.ReadLine());
m[i,j] = int.Parse(Console.ReadLine());
}
}
}
}
return m;
return m;
}
}
- 15. โปรแกรมตัว อย่า ง
ตัวอย่างข้างล่างแสดงการเรียกใช้เมท็
อด ReadMatrix และ ShowMatrix
แสดงเฉพาะส่วน Main เท่านั้น
static void Main() {
static void Main() {
int[,] A;
int[,] A;
Console.Write("Enter #rows: ");
Console.Write("Enter #rows: ");
int nrows = int.Parse(Console.ReadLine());
int nrows = int.Parse(Console.ReadLine());
Console.Write("Enter #columns: ");
Console.Write("Enter #columns: ");
int ncols = int.Parse(Console.ReadLine());
int ncols = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Matrix A");
Console.WriteLine("Enter Matrix A");
A = ReadMatrix(nrows, ncols);
A = ReadMatrix(nrows, ncols);
Console.WriteLine("Matrix A is");
Console.WriteLine("Matrix A is");
ShowMatrix(A);
ShowMatrix(A);
}
}
- 17. การบวกเมตริก ซ์
เมท็อด AddMatrix ทำาการบวกสองเมตริ
กซ์ และส่งผลลัพธ์เป็นเมตริกซ์ใหม่
โดยสมมติว่าเมตริกซ์ทั้งสองมีขนาดเท่ากัน
static int[,] AddMatrix(int[,] a, int[,] b) {
static int[,] AddMatrix(int[,] a, int[,] b) {
int nr = a.GetLength(0);
int nr = a.GetLength(0);
int nc = a.GetLength(1);
int nc = a.GetLength(1);
int[,] result = new int[nr,nc];
int[,] result = new int[nr,nc];
for (int i = 0; i < nr; i++) {
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
for (int j = 0; j < nc; j++) {
result[i,j] = a[i,j] + b[i,j];
result[i,j] = a[i,j] + b[i,j];
}
}
}
}
return result;
return result;
}
}
- 18. การคูณ เมตริก ซ์
A เป็นเมตริกซ์ขนาด m×n และ B เป็นเมตริกซ์
(A× )[i, ]
ขนาด Bn×jp= A[i,1]×B[1,j] + A[i,2]×B[2,j] + ... + A[i,n]×B[n,j]
เมท็อด MulMatrix ทำาการคูณเมตริกซ์ และ
ส่งผลลัพธ์ในเมตริกซ์ใหม่
สมมติว่า #cols ของเมตริกซ์แรกมีค่าเท่ากับ #rows
static int[,] MulMatrix(int[,] a, int[,] b) {
static int[,] MulMatrix(int[,] a, int[,] b) {
ของเมตริ = new อง
ี่
int[,] resultกซ์ทสint[a.GetLength(0), b.GetLength(1)];
int[,] result = new int[a.GetLength(0), b.GetLength(1)];
}
}
for (int i = 0; i < a.GetLength(0); i++) {
for (int i = 0; i < a.GetLength(0); i++) {
for (int j = 0; j < b.GetLength(1); j++) {
for (int j = 0; j < b.GetLength(1); j++) {
int sum = 0;
int sum = 0;
for (int k = 0; k < a.GetLength(1); k++)
for (int k = 0; k < a.GetLength(1); k++)
sum += a[i,k]*b[k,j];
sum += a[i,k]*b[k,j];
result[i,j] = sum;
result[i,j] = sum;
}
}
}
}
return result;
return result;