// This program implements strates various ways
// Create and manipulate
Array types.
//
// Comment/uncomment method callin main ()
//
Test.
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace funwitharrays
{
Class Program
{
Static void
Main (string [] ARGs)
{
Console. writeline ("***** fun with Arrays
*****");
Simplearrays ();
Arrayinitialization ();
Arrayofobjects ();
Rectmultidimensionalarray ();
Jargedmultidimensionalarray ();
Passandreceivearrays ();
Systemarrayfunctionality ();
Console. Readline ();
}
# Region helper Methods
Static void simplearrays ()
{
Console. writeline ("=> simple array creation .");
// Create
And fill an array of 3 Integers
Int [] myints = new int [3];
Myints [0] = 100;
Myints [1] = 200;
Myints [2] = 300;
// Now print each value.
Foreach (int I in myints)
Console. writeline (I );
Console. writeline ();
}
Static void arrayinitialization ()
{
Console. writeline ("=> array initialization .");
// Array
Initialization syntax using the new keyword.
String [] stringarray = new
String [] {"one", "two", "three "};
Console. writeline ("stringarray has
{0} elements ", stringarray. Length );
// Array initialization syntax without using the new keyword.
Bool [] boolarray = {false, false, true };
Console. writeline ("boolarray
Has {0} elements ", boolarray. Length );
// Array initialization with New Keyword and size.
Int []
Intarray = new int [4] {20, 22, 23, 0 };
Console. writeline ("intarray
Has {0} elements ", intarray. Length );
Console. writeline ();
}
Static void arrayofobjects ()
{
Console. writeline ("=>
Array of objects .");
// An array of objects can be anything at all.
Object []
Myobjects = new object [4];
Myobjects [0] = 10;
Myobjects [1] =
False;
Myobjects [2] = new datetime (1969, 3, 24 );
Myobjects [3]
= "Form & void ";
Foreach (Object OBJ in myobjects)
{
// Print
Type and value for each item in array.
Console. writeline ("type: {0 },
Value: {1} ", obj. GetType (), OBJ );
}
Console. writeline ();
}
Static void
Rectmultidimensionalarray ()
{
Console. writeline ("=>
Rectangular multidimen1_array .");
// A rectangular MD
Array.
Int [,] mymatrix;
Mymatrix = new int [6, 6];
// Populate (6*6) array.
For (INT I = 0; I <6;
I ++)
For (Int J = 0; j <6; j ++)
Mymatrix [I, j] = I *
J;
// Print (6*6) array.
For (INT I = 0; I <6;
I ++)
{
For (Int J = 0; j <6; j ++)
Console. Write (mymatrix [I, j] + "\ t ");
Console. writeline ();
}
Console. writeline ();
}
Static void jagedmultidimensionalarray ()
{
// Multi-dimensional array
Console. writeline ("=> jarged multidimeneline
Array .");
// A jagged MD array (I. e., an array of arrays ).
//
Here we have an array of 5 different arrays.
Int [] [] myjagarray = new
Int [5] [];
// Create the jarged array.
For (INT I = 0; I <
Myjaarray. length; I ++)
Myjaarray [I] = new int [I + 7];
// Print each row (Remember, each element is defaulted
Zero !)
For (INT I = 0; I <5; I ++)
{
For (Int J
= 0; j <myjaarray [I]. length; j ++)
Console. Write (myjaarray [I] [J] + "");
Console. writeline ();
}
Console. writeline ();
}
Static void printarray (INT [] myints)
{
For (INT I = 0; I
<Myints. length; I ++)
Console. writeline ("item {0} is {1}", I,
Myints [I]);
}
Static string [] getstringarray ()
{
String [] thestrings =
{"Hello", "from", "getstringarray "};
Return thestrings;
}
Static void passandreceivearrays ()
{
Console. writeline ("=> arrays as Params and return values .");
Int []
Ages = {20, 22, 23, 0 };
Printarray (AGEs );
String [] STRs =
Getstringarray ();
Foreach (string s in STRs)
Console. writeline (s );
Console. writeline ();
}
Static void systemarrayfunctionality ()
{
Console. writeline ("=> working with system. array .");
// Initialize
Items at startup.
String [] gothicbands = {"tones on tail", "Bauhaus ",
"Sisters of Mercy "};
// Print out names in declared order.
Console. writeline ("
-> Here is the array :");
// Getupperbound
For (INT I =
0; I <= gothicbands. getupperbound (0); I ++)
{
// Print
Name
Console. Write (gothicbands [I] + "");
}
Console. writeline ("\ n ");
// Reverse them...
// Reverse the Array
Array. Reverse (gothicbands );
Console. writeline ("-> the reversed
Array ");
//... And print them.
For (INT I = 0; I <=
Gothicbands. getupperbound (0); I ++)
{
// Print
Name
Console. Write (gothicbands [I] + "");
}
Console. writeline ("\ n ");
// Clear out all but the final member.
Console. writeline ("
-> Cleared out all but one ...");
Array. Clear (gothicbands, 1,
2 );
For (INT I = 0; I <= gothicbands. getupperbound (0); I ++)
{
// Print a name
Console. Write (gothicbands [I] +"
");
}
Console. writeline ();
}
# Endregion
}
}