Arrays – Data Structure and Algorithm using c#

In this article, you will learn the Array data structure – a straightforward and powerful data structure that can store and arrange data in a specific format to fix some type of complex problems. You can use it to store many variables of the same type, such as int, string, etc. We will also look at its different types, its advantages and dis-advantages etc.

What is an Array?

Imagine, you have an ice-cream shop and you need to show customers the numbers of flavors like “Strawberry”, “Chocolate”, “Vanilla” etc.

You can represent each different flavour in a separate variable to show like shown below:

1
2
3
4
5
String s1 = "Strawberry";
 
String s2 = "Chocolate";
 
String s3 = "Vanilla";

But, what if you have 100+ flavours available? Do you think, practically it will be possible to store each flavour in those many different variables?

Obviously, it will be a very bad way of representing and storing data in that way. So, here “Array” solve the above problem.

“An array is a list of items, a bit like an ice-cream flavor list. It allows you to store more than one item in only one variable.”

Array Basics

In programming terminology, an array is a container object that holds a fixed number of values of same type where each value has an ‘index’ or location associated with it. The length of an array is established when the array is created. After creation, its length is fixed.

  • Each item in an array is called an element.
  • Each element is accessed by its numerical index value.

The above problem can be represented using array like shown below:

1
String[] flavours = {"Strawberry","Chocolate","Vanilla"};

Types of array

We can categorise array into the following groups:

  1. One-Dimensional Array
  2. Multi-Dimensional Array
One-Dimensional Array

A single-dimensional array stores a collection of items of the same type, which are accessible by an index. The indices of arrays in C# are zero-based. This means that the first element has an index equal to 0, while the last one—length of the array minus one.

How to Create an One-Dimensional Array in C#

The declaration is very simple, because you just need to specify a type of element and a name, as follows:

1
2
3
4
5
6
7
// declares an array of integers
 
int[] anArrayOfIntegers;
 
// declares an array of String
 
String[] anArrayOfStrings;

Array Initialization:

To initialize the array elements to default values, you can use the new operator, as shown here:

// create an array of integers
 
anArrayOfIntegers = new int[5];

Alternatively, you can use the shortcut syntax to create and initialize an array:

1
int[] anArrayOfIntegers = {100, 200, 300400, 500};

Here the length of the array is determined by the number of values provided between braces and separated by commas.

you also can set the values of particular elements using the [] operator and an index of an element, as shown in the following code snippet:

1
2
3
anArrayOfIntegers[0] = 100;
 
anArrayOfIntegers[1] = 200;

What is important is that the number of elements in an array cannot be changed after initialization. For this reason, you will not be able to easily add a new item at the end of the array or insert it in a given position within the array.  If you need such features, you can use other data structures such as generic lists.

Multi-Dimensional Array

Two-dimensional array is another way of representing your data to solve a list of problems where we arrange data in rows and columns form.

two dimensional array in c#

 

Let’s take a look at an example regarding the declaration and initialization of multi-dimensional arrays.

Initialization:

// Two-dimension array with 4 rows and 2 columns

1
2
3
4
int[,] nums = new int[4, 2];  
 
// Three-dimension array with 4 rows and 2 columns
int[, ,] nums = new int[5, 4, 2];

Declaration:
you can also combine a declaration with an initialization, as shown below:

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

Accessing Elements:

You can access the multi-dimensional array element using indexes values of rows and columns.
Let’s take a look at the following example:

1
2
int num = nums[4][1]; 
nums[4][1] = 8;

nums[4][1] – the value from the 5th row and second column  is accessed.

 

Rahul is a Data Geek, technology enthusiast, a passionate writer, thinker with passion for computer programming.  He loves to explore technology and finds ultimate joy when writing about trending technology, geek stuff and web development.