What are string arrays in Solidity?
An array is a data structure that stores multiple variable values of similar data type under a single variable name. Memory allocation in arrays is contiguous, allowing array elements to be accessed via indexes. String arrays hold elements of type string and can be of fixed or dynamic size depending on the program’s requirement.
Declaration and initialization of arrays
Fixed size arrays
In fixed size arrays, the size of the array is provided during declaration and must be a positive integer. The number of values given in initialization are less than or equal to the size of array specified.
Syntax
string [size] <name of array> = <initialization>;
Example
pragma solidity ^0.5.0;contract FixedSizeArray {string [5] fixedArray1=["Apple", "Mango", "Banana", "Orange", "Grapes"];}
Explanation
Line 1: Pragma directive is the first line in any solidity program and indicates the compiler version to be used for the given file.
Line 3: A contract is made.
Line 5: An array of fixed size 5 is declared and initialized.
Dynamic size arrays
In dynamic size arrays, the size of the array is not pre-defined rather grows during the execution of code.
Syntax
string [] <name of array> = <initialization>;
Example
pragma solidity ^0.5.0;contract DynamicSizeArray {string [] dynamicArray= ["Apple", "Mango", "Banana", "Orange", "Grapes"];}
Explanation
Line 1: Pragma directive is the first line in any solidity program and indicates the compiler version to be used for the given file.
Line 3: A contract is made.
Line 5: An array of dynamic size is declared and initialized without size which will be determined from initialization.
Accessing array elements
As memory allocation in arrays is contiguous, array elements are accessed through indexes with the smallest index 0 pointing towards the first element and the largest index array.length-1 pointing towards the last element. Thus for accessing ith element (i-1)th index is used.
Syntax
string <variable name> = <name of array> [index];
Example
pragma solidity ^0.5.0;contract AccessArray {string [5] fruits=["Apple", "Mango", "Banana", "Orange", "Grapes"];string myFruit= fruits[1];}
Explanation
Line 1: Pragma directive is the first line in any solidity program and indicates the compiler version to be used for the given file.
Line 3: A contract is made.
Line 5: An array of fixed size 5 is declared and initialized.
Line 7: 1st index of the array fruits is accessed which will be mango and is assigned to variable myFruit.
Length
The length function is used to determine the size of an array.
Syntax
uint <new length> = <name of array>.length;
Example
pragma solidity ^0.5.0;contract LengthOfArray {string [] fruits=["Apple", "Mango", "Banana", "Orange", "Grapes"];uint len= fruits.length;}
Explanation
Line 1: Pragma directive is the first line in any solidity program and indicates the compiler version to be used for the given file.
Line 3: A contract is made.
Line 5: A dynamic array is declared and initialized.
Line 7: Length function is used to determine length of the array which is stored in the variable len.
Push()
The push function is used to append a new element in the dynamic array. It returns the new length of array.
Syntax
uint <new length>= <name of array>.push("data");
Example
pragma solidity ^0.5.0;contract PushFunction {string [] fruits=["Apple", "Mango", "Banana"];uint newLength= fruits.push("Orange");}
Explanation
Line 1: Pragma directive is the first line in any solidity program and indicates the compiler version to be used for the given file.
Line 3: A contract is made.
Line 5: A dynamic array is declared and initialized.
Line 7: "Orange" is pushed in the array at index 3 and length will be updated to 4 which will be assigned to variable newLength.