In any programming language, array inherits a special attention. It is a data structure that is heavily used throughout coding. Arrays in Ballerinalang is mutable. If an object can be altered after initiating that object, such objects are called mutable objects. When an array becomes mutable, the size of the array can be changed dynamically. If you prefer fixed length arrays, the option is available. Called as sealable arrays. If an array is a sealed array, then the length cannot be changed once it is initialized. However the values storing in an array should belong to a same type.

## Declaration and Initialization

There are two approaches to declare an array. First it is required to decide if the array is a fixed length array or a variable length array, in other words, an immutable or mutable array. Immutable arrays are commonly referred to as sealable arrays in Ballerinalang. The type of the values the array holds needs to defined first. Then the length, if the array is a sealable array. Default initialization length of the array is zero length. Ballerina supports multi-dimensional arrays as well.

import ballerina/io;
function main(string... param){
int[] real_numbers;
int[] numbers = [];
int[] odd_numbers = [1,3,5,7,9];
int[5] even_numbers = [0,2,4,6,8];
int[][] pascal_numbers = [[1], [1,1], [1,2,1], [1,3,3,1]];
io:println("Real Numbers - ",real_numbers);
io:println("Numbers - ",numbers);
io:println("Odd Numbers - ",odd_numbers);
io:println("Even Numbers - ",even_numbers);
io:println("Pascal Numbers - ",pascal_numbers);
numbers[0] = 100;
io:println("Numbers - ",numbers);
}

**Output**

`Real Numbers - []`

Numbers - []

Odd Numbers - [1, 3, 5, 7, 9]

Even Numbers - [0, 2, 4, 6, 8]

Pascal Numbers - [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]

Numbers - [100]

Above example shows different ways of declaring and initializing arrays. The index starts in 0. Therefore the last index + 1 becomes the size of the array.

## Length of arrays

When working with arrays the length of the array is very important parameter. In order to find the length of an array, **lengthof** keyword can be used.

import ballerina/io;
function main(string... param){
int[][] pascal_numbers = [[1], [1,1], [1,2,1], [1,3,3,1]];
io:println("Numbers - ",numbers);
}

**Output**

`Length 4`

## Mutable Arrays

Following is a demonstration on mutability of arrays.

import ballerina/io;
function main(string... param){
int[] real_numbers;
io:println("Real Numbers - ", real_numbers);
io:println("Real Numbers (length)- ", lengthof real_numbers);
real_numbers[1] = 1;
real_numbers[3] = 3;
real_numbers[5] = 5;
io:println("Real Numbers - ", real_numbers);
io:println("Real Numbers (length)- ", lengthof real_numbers);
real_numbers[6] = 6;
real_numbers[8] = 8;
real_numbers[10] = 10;
io:println("Real Numbers - ", real_numbers);
io:println("Real Numbers (length)- ", lengthof real_numbers);
}

**Output**

`Real Numbers - []`

Real Numbers (length)- 0

Real Numbers - [0, 1, 0, 3, 0, 5]

Real Numbers (length)- 6

Real Numbers - [0, 1, 0, 3, 0, 5, 6, 0, 8, 0, 10]

Real Numbers (length)- 11

The length of the array changes as the values are assigned.

Fixed Length Arrays

If mutable arrays are not desired, then the array can be sealed. It is simple as including the length of the array at the time of defining it.

import ballerina/io;
function main(string... param){
string[5] vowels;
vowels[0] = "a";
vowels[1] = "e";
vowels[2] = "i";
vowels[3] = "o";
vowels[4] = "u";
io:println("Vowels in the alphabet - ", vowels);
}

**Output**

`Vowels in the alphabet - ["a", "e", "i", "o", "u"]`

A compilation error occurs when tries to add a value outside the range of the array.

import ballerina/io;
function main(string... param){
string[5] vowels;
vowels[0] = "a";
vowels[1] = "e";
vowels[2] = "i";
vowels[3] = "o";
vowels[4] = "u";
vowels[5] = "A";
io:println("Vowels in the alphabet - ", vowels);
}

**Output**

`error: .\arr.bal:10:12: array index out of range: index: '5', size: '5' compilation contains errors`

How to define a float type mutable array of length of 10?

In case it is required define an array with temporary fixed length but with the flexibility of changing it later, if required.

import ballerina/io;
function main(string... param){
float[] point_numbers;
point_numbers[9] = 0.0;
io:println("Length of the array - ", lengthof point_numbers);
io:println("Length of the array - ", point_numbers);
}

**Output**

`Length of the array - 10`

Length of the array - [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Note when the final element is defined the other elements are getting initialized automatically inferring the default value which belongs to the type.