Arrays
Written by: maffelu , 2009-05-16 16:15:32
class TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array
int[] array1 = new int[5];
// Declare and set array element values
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// Declare a two dimensional array
int[,] multiDimensionalArray1 = new int[2, 3];
// Declare and set array element values
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// Declare a jagged array
int[][] jaggedArray = new int[6][];
// Set the values of the first array in the jagged array structure
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
}
}
string MyNames = "John, Michael, Anna, Lisa";
string[] MyArrayNames = new string[4];
MyArrayNames[0] = "John";
MyArrayNames[1] = "Michael";
MyArrayNames[2] = "Anna";
MyArrayNames[3] = "Lisa";
Console.WriteLine("The names in string form:");
Console.WriteLine(MyNames);
Console.WriteLine("");
Console.WriteLine("The names in array form:");
foreach(string s in MyArrayNames)
{
Console.WriteLine(s.ToString());
}
Console.Read();
Console.WriteLine("The women only:");
Console.WriteLine(MyArrayNames[2]);
Console.WriteLine(MyArrayNames[3]);
string[] MyArrayNames = new string[] {"John", "Michael", "Anna", "Lisa"};
public static void Main(string[] args)
{
//Declaring the content directly, no index is needed
string[,] MyTwoDimArray = new string[,] {{"Michael", "Johnsson"}, {"John", "Doe"}};
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
Console.Write(MyTwoDimArray[i,j]+" ");
}
Console.WriteLine("");
}
Console.WriteLine("");
//Declaring only the array, with index, and contents later
string[,] MyOtherArr = new string[2,2];
MyOtherArr[0,0] = "Anna";
MyOtherArr[0,1] = "Thomsson";
MyOtherArr[1,0] = "Lisa";
MyOtherArr[1,1] = "Dumblebore";
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
Console.Write(MyOtherArr[i,j]+" ");
}
Console.WriteLine("");
}
Console.Read();
ListBox[] ctrArray = new ListBox[5];
ctrArray[0] = lsbIndex1;
ctrArray[1] = lsbIndex2;
ctrArray[2] = lsbIndex3;
ctrArray[3] = lsbIndex4;
ctrArray[4] = lsbIndex5;
ctrArray[3].Items.Add("test");
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.