namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonArray1D_Click(object sender, EventArgs e)
{
/* ----- Example of Array 1D ----- */
int[] a = new int[6] { 2, 4, 6, 8, 10, 12 };
string strOut = "";
int i = 0;
foreach (int temp in a)
{
strOut += temp.ToString() + "\n";
i++;
}
strOut += "Length = " + (a.GetLength(0)).ToString();
MessageBox.Show(strOut, "Array 1D");
}
private void buttonArray2DDefine_Click(object sender, EventArgs e)
{
/* ----- Example of Array 2D แบบกำหนดค่า ----- */
int[,] b = new int[3, 2] { {1,2},
{7,8},
{4,5} };
string strOut = "";
for (int i = b.GetLowerBound(0); i <= b.GetUpperBound(0); i++)
{
for (int j = b.GetLowerBound(1); j <= b.GetUpperBound(1); j++)
{
strOut += "[" + i.ToString() + "," + j.ToString() + "] = " + b[i, j].ToString() + " ";
}
strOut += "\n";
}
MessageBox.Show(strOut, "Array 2D Define");
}
private void buttonArray2DRandom_Click(object sender, EventArgs e)
{
/* ----- Example of Array 2D แบบ Random ค่า ----- */
int[,] b = new int[4, 3];
Random rnum = new Random();
string strOut = "";
for (int i = b.GetLowerBound(0); i <= b.GetUpperBound(0); i++)
{
for (int j = b.GetLowerBound(1); j <= b.GetUpperBound(1); j++)
{
b[i, j] = rnum.Next(10);
strOut += "["+i.ToString()+","+j.ToString()+"] = "+b[i,j].ToString()+" ";
}
strOut += "\n";
}
MessageBox.Show(strOut, "Array 2D Random");
}
private void buttonArray3D_Click(object sender, EventArgs e)
{
/* ----- Example of Array 3D ----- */
int[, ,] c = new int[2, 2, 2] { { {1, 2}, {3, 4} },
{ {5, 6}, {7, 8} } };
string strOut = "";
for (int i = c.GetLowerBound(0); i <= c.GetUpperBound(0); i++)
{
for (int j = c.GetLowerBound(1); j <= c.GetUpperBound(1); j++)
{
for (int k = c.GetLowerBound(2); k <= c.GetUpperBound(2); k++)
{
strOut += "[" + i.ToString() + "," + j.ToString() + "," + k.ToString() + "] = " + c[i, j, k].ToString() + " ";
}
}
strOut += "\n";
}
MessageBox.Show(strOut, "Array 3D");
}
private void buttonArrayJag_Click(object sender, EventArgs e)
{
int[][] jg = new int[2][];
jg[0] = new int[4] { 4, 2, 1, 7 };
jg[1] = new int[3];
// กำหนดค่า random ให้ jg[1]
Random r = new Random();
for (int i = 0; i < jg[1].Length; i++)
{
jg[1][i] = r.Next(10);
}
string strOut = "";
int n = 0;
foreach (int j in jg[0])
{
strOut += "สมาชิกของ jg [0][" + n.ToString() + "] = " + j.ToString() + "\n";
n++;
}
MessageBox.Show(strOut, "Jag Array สมาชิกตัวที่ 1");
strOut = "";
for (int j = jg[1].GetLowerBound(0); j <= jg[1].GetUpperBound(0); j++)
{
strOut += "สมาชิกของ jg [1][" + j.ToString() + "] = " + j.ToString() + jg[1][j].ToString() + "\n";
}
MessageBox.Show(strOut, "Jag Array สมาชิกตัวที่ 2");
}
}
}
Sign up here with your email

ConversionConversion EmoticonEmoticon