Array data structure

From Wikipedia, the free encyclopedia
(Redirected from Array)
Jump to navigation Jump to search

In programming languages, an array is a way of storing several items (such as integers). In some programming languages, the contents must have the same type (only integers, only strings, …) and cannot store a mix of types. However, languages such as JavaScript do allow multiple types within arrays. 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 other languages, the first item has index 1 (and then 2, 3, …).

When the programmer creates an array, they 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 they must create a new array. This is because the size of an array can not be changed. Types of arrays in C is here

Arrays in C[change | change source]

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[change | change source]

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 JavaScript[change | change source]

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

var arrayNameHere = [element1, element2, element3, /* and so on... */ ];

This creates an array, and the array is more flexible than C and Java, allowing multiple types to be stored inside. The programmer can now access the array by doing this:

1 var array = [ 1, true, variable1, 33, "Hello" ];
2 
3 array[0] = 1;
4 array[1] = true;
5 array[2] = variable1;
6 array[3] = 33;
7 array[4] = "Hello";

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

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

Mathematic operators can operate on numbers only.