Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Truy cập phần tử từ mảng hình chữ nhật (Rectangular array) trong C#


Mảng đa chiều (rectangular array) truy cập bằng index a[0,1] (phần tử thứ 2 hàng 0).

Ví dụ

using System;
namespace Demo {
    class Program {
        static void Main(string[] args) {
            int[,] a = new int[3,3];
            a[0,0]=10; a[0,1]=20; a[0,2]=30;
            a[1,0]=40; a[1,1]=50; a[1,2]=60;
            a[2,0]=70; a[2,1]=80; a[2,2]=90;
            for (int i=0;i<3;i++) for(int j=0;j<3;j++) Console.WriteLine("a[{0},{1}] = {2}", i,j,a[i,j]);
            int ele = a[0,1];
            Console.WriteLine("Phần tử thứ 2 hàng 0: " + ele);
            Console.ReadKey();
        }
    }
}