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

Thêm phần tử vào Jagged Array hiện có trong C#


Để thêm/sửa phần tử trong jagged array, gán giá trị mới tại index.

a[3][1] = 500;

Truy cập phần tử đầu tiên của mảng con thứ 3.

Ví dụ

using System;
namespace Demo {
    class Program {
        static void Main(string[] args) {
            int[][] x = new int[][] { new int[]{10,20}, new int[]{30,40}, new int[]{50,60}, new int[]{70,80}, new int[]{90,100} };
            int i, j;
            Console.WriteLine("Mảng cũ...");
            for (i = 0; i < 5; i++) for (j = 0; j < 2; j++) Console.WriteLine("a[{0}][{1}] = {2}", i, j, x[i][j]);
            x[3][1] = 500;
            Console.WriteLine("Mảng mới...");
            for (i = 0; i < 5; i++) for (j = 0; j < 2; j++) Console.WriteLine("a[{0}][{1}] = {2}", i, j, x[i][j]);
            Console.ReadKey();
        }
    }
}