.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

การใช้งาน Array Options · View
paedotnet
Posted: Friday, December 21, 2007 3:05:44 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk

การใช้งาน 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();
    }
}

ผลลัพธ์แสดงดังรูป

รูปที่ 1

การใช้งาน 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]
Basketman
Posted: Monday, December 24, 2007 8:10:38 AM

Rank: มือเทพ
Groups: Member

Joined: 12/9/2007
Posts: 104
Location: THA

ขอ parameter array ด้วยงับ



i wanna be da Developer man~!!!! how??
paedotnet
Posted: Monday, December 24, 2007 3:41:13 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk

Parameter Array

using System;
public class TestParameterArray
{
    public static void Param_Array(params int[] value)
    {
        Console.WriteLine("Total Length  : " + value.Length);
        foreach (int i in value)
        {
            Console.WriteLine("Member is : "+i);
        }
        Console.WriteLine();
    }
    public static void Main()
    {
        int[] i = { 5, 6, 7, 4, 2, 5, 7 };
        int[] j = { 44, 55 };
        int[] k = { 8 };
        Param_Array(i);
        Param_Array(j);
        Param_Array(k);
        Console.ReadLine();
    }
}

 

อีกตัวอย่างนะครับแต่ต้วอย่างนี้ไม่ได้ใช้ params

 

using System;
public class TestArray
{
    public void ShowArray(int[] value)//รับค่าเข้ามาเป็นอาร์เรย์ชนิด int
    {
        for (int i = 0; i < value.Length; i++)
        {
            Console.WriteLine(value[i] + "\t");
        }
        Console.WriteLine();
    }
    public void ShowArrayObject(object[] obj)//รับค่าเข้ามาเป็นชนิด object
    {
        for (int i = 0; i < obj.Length; i++)
        {
            Console.WriteLine(obj[i] + "\t");
        }
        Console.WriteLine();
    }
    public static void Main()
    {
        TestArray a = new TestArray();
        a.ShowArray(new int[] { 3, 4, 6, 8, 9, 33 });
        a.ShowArrayObject(new string[] { "one", "two", "three", "four", "five" });
        Console.ReadLine();
    }
}

 

 



[With great power comes great responsibility]
Basketman
Posted: Tuesday, December 25, 2007 12:30:59 AM

Rank: มือเทพ
Groups: Member

Joined: 12/9/2007
Posts: 104
Location: THA

 ขอบคุณครับ



i wanna be da Developer man~!!!! how??
nikorn
Posted: Tuesday, July 29, 2008 10:17:49 PM
Rank: มือสมัครเล่น
Groups: Member

Joined: 7/14/2008
Posts: 10
Location: good

หวัดดีครับ คือผมต้องการอ่านข้อมูลมาจากฐานข้อมูลแล้วนำมาเก็บไว้ใน Array ทีละแถวนะครับ ช่วยตอบผมทีนะครับผม

 

string strConn;
string sqlPlant;

strConn = WebConfigurationManager.ConnectionStrings["diagnose"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();

sqlPlant = "Select * from durien";
SqlDataAdapter da4 = new SqlDataAdapter(sqlPlant, Conn);

DataSet ds4 = new DataSet();
da4.Fill(ds4, "durien");

 

int counrows = ds4.Tables[0].Columns.Count; //นับจำนวน Rows ทั้งหมดมีกี่ แถว
int countcolum = ds4.Tables[0].Rows.Count;

for (int x = 0; x < countcolum ; x++){ // ให้+ ค่าไปเรื่อยๆๆจนกว่า มีค่ามากหรือเท่ากับค่า Rows
for (int y = 2; y < counrows; y++){


string id = ds4.Tables[0].Rows[x][y].ToString(); // ค้นหาแต่ล่ะ Rows
int result = int.Parse(id);

// Label1.Text += result.ToString();

}

string[] rule;
rule = new string[x];

// rule[x] += x.ToString();



}

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by