Professional C# 2008 phần 2

Mảng thông thường (còn được gọi là mảng 1 chiều) được lập chỉ mục của một số nguyên duy nhất. Một mảng đa chiều được lập chỉ mục của hai hay nhiều số nguyên. Hình 5-3 cho thấy các ký hiệu toán học cho một mảng 2 chiều có ba hàng và ba cột. Hàng đầu tiên có giá trị 1, 2, và 3, và hàng thứ ba trong 7 giá trị, 8, và 9 | Chapter 5 Arrays Multidimensional Arrays Ordinary arrays also known as 1-dimensional arrays are indexed by a single integer. A multidimensional array is indexed by two or more integers. Figure 5-3 shows the mathematical notation for a 2-dimensional array that has three rows and three columns. The first row has the values 1 2 and 3 and the third row has the values 7 8 and 9. a ri 2 3 4 5 6 l_7 8 9_ Figure 5-3 Declaring this 2-dimensional array with C is done by putting a comma inside the brackets. The array is initialized by specifying the size of every dimension also known as rank . Then the array elements can be accessed by using two integers with the indexer int twodim new int 3 3 twodim 0 0 1 twodim 0 1 2 twodim 0 2 3 twodim 1 0 4 twodim 1 1 5 twodim 1 2 6 twodim 2 0 7 twodim 2 1 8 twodim 2 2 9 You cannot change the rank after declaring an array. You can also initialize the 2-dimensional array by using an array indexer if you know the value for the elements in advance. For the initialization of the array one outer curly bracket is used and every row is initialized by using curly brackets inside the outer curly brackets. int twodim 1 2 3 4 5 6 7 8 9 When using an array initializer you must initialize every element of the array. It is not possible to leave the initialization for some values. By using two commas inside the brackets you can declare a 3-dimensional array int threedim 1 2 3 4 5 6 7 8 9 10 11 12 threedim 0 1 1 125 Part I The C Language Jagged Arrays A 2-dimensional array has a rectangular size for example 3 X 3 elements . A jagged array is more flexible in sizing the array. With a jagged array every row can have a different size. Figure 5-4 contrasts a 2-dimensional array that has 3 X 3 elements with a jagged array. The jagged array shown contains three rows where the first row has two elements the second row has six elements and the third row has three elements. A jagged array is declared by placing one pair of opening and closing .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.