Friday, July 19, 2013

Arrays

Declare you datatype with a set of braces affixed  - int[] x;
Then allocate the array  - int[] x = new int[3];
Assign the values  - 
int[] x = new int[3];
x[0] = 1;
x[1] = 2;
x[2] = 3;
x[2] = 3;//Try to add this one & it will crash

or

int[] x = new int[3] {1, 2, 3};

or just

int[] x = {1, 2, 3};

Access the elements

System.Console.WriteLine(x[1] * x[2]); // will output 6
----------------------------------------------------------------------------------------------------

There are two kinds of multidimensional arrays:

Rektangular                                                                                                                        string[,] x = new string[2, 2];
x[0, 0] = "00"; x[0, 1] = "01"; 
x[1, 0] = "10"; x[1, 1] = "11"; 

or

string[,] x = { { "00", "01" },
                      { "10", "11" }  };


Jagged

string[][] a = new array[2][];
a[0] = new array[1]; a[0][0] = "00";
a[1] = new array[2]; a[1][0] = "10"; a[1][1] = "11";

or


string[][] a = { new string[]  { "00", };

                 new string[]  { "00" , "00", }  };

#################################################################



















No comments:

Post a Comment