MorkaLork Development

Interesting stuff I've picked up over the years...

Arrays

2009-05-16 16:15:32 | 283 views | arrays csharp C# single-dimensional multi-dimensional jagged variables collection storing

The Basics




From MSDN

An array is a data structure that contains several variables of the same type. Arrays are declared with a type:

type[] arrayName;

The following examples create single-dimensional, multidimensional, and jagged arrays:


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 };
}
}


Arrays are ways to store data in separate compartments and by doing so creating easy access to the data. The differance between "1, 2, 3, 4"(string) and "1", "2", "3", "4"(sting array) is that if you want the string number "1" you can, with a string array, just select it by index whereas a string would have to first be split into an array, then select it by index.

Arrays will handle data better if you need to store alot of it with easy access.

Getting into it



Allright, let's take a look at the differance between storing in a string and storing in an array:



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();



Will output this:

The names in string and array form

As we can see, there is a great differance.

Now, if we would need only the two women in the array, we could do as follows:



Console.WriteLine("The women only:");
Console.WriteLine(MyArrayNames[2]);
Console.WriteLine(MyArrayNames[3]);


As we can see, we use the index to select what parts ot the array we want to use.

There is an alternative way of entering data into an array(hey, it rhymes).



string[] MyArrayNames = new string[] {"John", "Michael", "Anna", "Lisa"};


This way, we don't have to set an index for the array since we enter the data in the declaration of the array.

Advanced



There are two ways of handling multidimensional arrays in C#. You can either use a normal array and add another dimension, or use the list. I prefer list/arraylist, but if you want a 2D array, here goes:



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();


This should looks something like this:

The 2D array output


Control arrays



This is how to create control arrays in C#:



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");


You do it just like a normal array using the control of choice as datatype.


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.