Arrays, Array List, Generic List

Standard
Array (Single Dimensional)

Data structures that hold variable number of data values or elements referred by the common name is called Array. Values stored in one dimensional is one dimensional or single dimensional array. These are indexed by single integer.

1

2

45
3
6
23
using System;
using System.Collections;

namespace array
{
  class Program
{
     static void  Main(string [] args)
{
singleDimensionalArray sDA = new singleDimensionalArray();
sDA.displayArrayValue(sDA.assignArray());
sDA.displayArrayValue(sDA.sortArray(sDA.assignArray()));
sDA.displayArrayValue(sDA.reverseArray(sDA.assignArray()));
       Console.ReadLine();
}
}

public class  mySortClass : IComparer
{
       int IComparer.Compare(Object x, Object y)
{
             return ((newCaseInsensitiveComparer ()).Compare(x, y));
}
}

public class myReverseClass : IComparer
{
        int IComparer.Compare(Object x, Object y)
{
                 return ((new CaseInsensitiveComparer()).Compare(y, x));
}
}

public class singleDimensionalArray
{
mySortClass mSC = new mySortClass();
myReverseClass mRC = new myReverseClass(); // Create and assign values
public int[] assignArray() {
int[] intArray = new int[] { 1, 2, 5, 7, 8, 6, 4, 3 };
Array.Resize<int>(ref intArray, intArray.Length + 1);
intArray[intArray.Length – 1] = 9;
return intArray;
}

public int[] sortArray(int[] iArray)
{
Array.Sort(iArray, mSC);
return  iArray;
}
public int[] reverseArray(int[] iArray) { Array.Sort(iArray, mRC);
return iArray;
}

public void displayArrayValue(int[] iArray)
{
foreach (int Value in iArray)
{
Console.WriteLine(Value.ToString());
}
}
}
}

Array (Multidimensional) Array values stored in more than one dimensions is called multidimensional array. That means, these are indexed by more than one integer. Matrix with m number of rows and n number of columns.

public class multiDimensionalArray

{

// Create and assign values

public int[,] twoDimensionalArray()

{

int[,] intArray = new int[,] { {1, 2},{ 5, 7}, {8, 6}, {4, 3} };

return intArray;

}

public int[,,] threeDimensionalArray()

{

int[,,] intArray = new int[,,] {{{1, 2},{3, 4}},{{4, 5},{7,6 }},{{8,9},{6, 7}}};

return intArray;

}

}

Jagged Array:

C# allows to create unequal dimension of array. That means allows to create jagged array. This also called array of arrays.

  1. equal dimension multidimensional array declared as int [, ,]

  2. Unequal dimension jagged array declared as int [ ][ ]

  3. Jagged array mixed with multidimensional array int [ ] [ , ]

static int[][] jaggedArray()

{

int [][] iJaggedArray = newint [3] [];

iJaggedArray[0] = new int[]{1,2,3,4,5,6};

iJaggedArray[1] = new int[] { 7, 8, 9 };

iJaggedArray[2] = new int[5];

return iJaggedArray;

}

static int[][,] mixedArray()

{

int[][,] iMixedArray = new int[3][,]{

new int[,] { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 } },

new int[,] { { 5, 6 }, { 6, 7 } },

new int[,] { { 7, 8 }, { 8, 9 } }}

return iMixedArray; }

Array List ArrayList dynamically resizable as required. This implemented using IList interface, System.Collections member. Easy to add or remove the values in the ArrayList using ‘Add’, ‘AddRange’ or ‘Remove’, ‘RemoveAt’, ‘RemoveRange’ respectively.

using System;

using System.Collections;

namespace arrayList

{

class Program

{

static void Main(string[] args)

{

ArrayList AL = new ArrayList();

AL.Add(1); //Boxing

AL.Add(2);

AL.Add(3);

AL.Add(4);

AL.Add(5);

AL.RemoveRange(2, 2); //using index from to number of element

Console.WriteLine(“Array List size is {0}”, AL.Count);

AL.Remove(3); //using value

AL.RemoveAt(2); //using index

AL.Add(6);

foreach (int ar in AL)//Unboxing

{

Console.WriteLine(“Array List Value is {0}”, ar.ToString());

}

Console.ReadLine();

}

}

}

Boxing and Unboxing Boxing is the term used to denote the value type assigned into the object type. Value types are using stack memory. object types are using heap memory.

static ArrayList arrayBoxing()

{

ArrayList AL = new ArrayList(); //Array List is the class object

AL.Add(1); //integer value assigned to array list

AL.Add(2);

AL.Add(3);

AL.Add(4);

AL.Add(5);

return AL;

}

Unboxing is the term used to denote the reverse process of the boxing. Unboxing the array values only possible it should be boxed with array.

static void arrayUnboxing(ArrayList AL)

{

foreach (int ar in AL) //Array List elements into integer variable

{

Console.WriteLine(“Array List Value is {0}”, ar.ToString());

}

}

Boxing with different values type are accepted. That means accidental addition of other type values are accepted by array list without any warning or error. But unboxing those values in dynamic loop gives error.

static ArrayList arrayBoxingWithDifferentType()

{

ArrayList AL = new ArrayList(); //Array List is the class object

AL.Add(1); //integer value assigned to array list

AL.Add(2);

AL.Add(“stringValue”);

AL.Add(4.5);

AL.Add(true);

Console.WriteLine((bool)AL[4]);

return AL;

}

Unboxing array to integer type accepted with string, boolean values. But generate compile time error. System.InvalidCastException – Specified cast is not valid. Another one issue with this boxing and unboxing array list is performance.

static void arrayUnboxingWithDifferentTypesOfValue(ArrayList Al)

{

try

{

int j = 0;

foreach (int i in Al) //Unboxing

{

Console.WriteLine(“Unboxing array value {0} {1}”,j.ToString(),i.ToString());

j++;

}

}

catch (InvalidCastException ex)

{

Console.WriteLine(ex.ToString());

}

}

Generic List .Net is overcome the above drawbacks with Generic List. Able to declare specific type list. It is avoiding accidental addition of the other type values. IDE gives immediate warning, compiler gives error and its avoiding the runtime error such as InvalidCastException. It has Better performance compare with Array List (Sorry , I have not tested)

static List<int> genericList()

{

List<int> iGenericList = new List<int>();

iGenericList.Add(1);

iGenericList.Add(2);

return iGenericList;

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s