C# 0 The Complete Reference


Initializing Multidimensional Arrays



Download 4,07 Mb.
Pdf ko'rish
bet207/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   203   204   205   206   207   208   209   210   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Initializing Multidimensional Arrays

A multidimensional array can be initialized by enclosing each dimension’s initializer list 

within its own set of curly braces. For example, the general form of array initialization for a 

two-dimensional array is shown here:



type

[,]


array_name

 = {


    { 

val

,

val

,

val

, ..., 


val

 },


    { 

val

,

val

,

val

, ..., 


val

 },


.

.

.



    { 

val

,

val

,

val

, ..., 


val

 }

};



Here, 

val

 indicates an initialization value. Each inner block designates a row. Within each 

row, the first value will be stored in the first position, the second value in the second position

and so on. Notice that commas separate the initializer blocks and that a semicolon follows 

the closing 

}

.

www.freepdf-books.com




144

 

P a r t   I :  



T h e   C #   L a n g u a g e

For example, the following program initializes an array called 



sqrs

 with the numbers 1 

through 10 and their squares.

// Initialize a two-dimensional array.

using System;

class Squares {

  static void Main() {

    int[,] sqrs = {

      { 1, 1 },

      { 2, 4 },

      { 3, 9 },

      { 4, 16 },

      { 5, 25 },

      { 6, 36 },

      { 7, 49 },

      { 8, 64 },

      { 9, 81 },

      { 10, 100 }

    };

    int i, j;



    for(i=0; i < 10; i++) {

      for(j=0; j < 2; j++)

        Console.Write(sqrs[i,j] + " ");

      Console.WriteLine();

    }

  }


}

Here is the output from the program:

1 1

2 4


3 9

4 16


5 25

6 36


7 49

8 64


9 81

10 100


Jagged Arrays

In the preceding examples, when you created a two-dimensional array, you were creating 

what C# calls a 

rectangular array.

 Thinking of two-dimensional arrays as tables, a rectangular 

array is a two-dimensional array in which the length of each row is the same for the entire 

array. However, C# also allows you to create a special type of two-dimensional array called 

a

jagged array.

 A jagged array is an 



array of arrays

 in which the length of each array can differ. 

Thus, a jagged array can be used to create a table in which the lengths of the rows are not 

the same.

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   203   204   205   206   207   208   209   210   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish