Array data structure: Difference between revisions

From Simple English Wikipedia, the free encyclopedia
Content deleted Content added
Introduce array in c#
Line 66: Line 66:


strfruits[5]= pineapple Value in the 5 position of the array
strfruits[5]= pineapple Value in the 5 position of the array

[http://www.eisdevelopers.com/developerssite/.net-articles/array-in-c-sharp/ Basic Concept of Arrays in C#]

Revision as of 06:36, 3 April 2013

In programming languages, an array is a way of storing several items (such as integers). These items must have the same type (only integers, only strings, ...) because an array can not store different kinds of items. Every item in an array has a number so the programmer can get the item by using that number. This number is called the index. In some programming languages, the first item has index 0, the second item has index 1 and so on. But in some languages, the first item has index 1 (and then 2, 3, ...).

When the programmer creates an array, he/she must give the size of the array. This is the number of items that can be stored in the array. If the programmer wants to store more items then he/she must create a new array. This is because the size of an array can not be changed.

Arrays in C

In the programming language C, arrays can be created like this:

int array[5];

This creates an array of integers and it can store 5 integers. The programmer can now store integers in the array by doing:

array[0] = 1;
array[1] = 18;
array[2] = 5;
array[3] = 33;
array[4] = 50;

The programmer can use a value in the array like this:

int k = 3 + array[3];  // k is now 3 + 33 = 36

Arrays in Java

In the programming language Java, arrays can be created like this:

int[] array = new int[5];

This creates an array of integers and it can store 5 integers. The programmer can now store integers in the array by doing:

array[0] = 1;
array[1] = 18;
array[2] = 5;
array[3] = 33;
array[4] = 50;

The programmer can use a value in the array like this:

int k = 3 + array[3];  // k is now 3 + 33 = 36

Arrays in C#

It is collection of values of similar data type. The size of an array is fixed and must be define before using. C# arrays are zero indexed; that is, the array indexes start at zero.

Syntax:- < data type>[ ] < identifier >=new < data type > [ < size of array > ];

Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences that you should be aware of. When declaring an array, the square brackets ([]) must come after the type, not the identifier. Placing the brackets after the identifier is not legal syntax in C#.


string [] strfruits={"mango","banana","apple","orange","pineapple"}

strfruits[0]= mango Value in the 0 position of the array

strfruits[1]= banana Value in the 1 position of the array

strfruits[2]= apple Value in the 2 position of the array

strfruits[3]= papaya Value in the 3 position of the array

strfruits[4]= orange Value in the 4 position of the array

strfruits[5]= pineapple Value in the 5 position of the array

Basic Concept of Arrays in C#