Semester: - VII Subject: Blockchain AY: 2024-25
Arrays in Solidity
The Arrays in Solidity can be classified into the following two types based on size –
• Fixed Size Array
• Dynamic Array
1. Fixed Size Array: In a fixed size array, the size of the array has to be specified during
declaration. Once declared, the array size cannot be changed. Any attempt to
reference beyond the last array position will result in an error. The fixed size array can
be initialized either inline during declaration or after the declaration in another function
as shown in the below example. A value can be assigned to a specific position of the
array by using the index of that position as shown in the third initialization example.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract
{
// Inline Initialization
uint[3] public age = [8,15,32];
string[3] public name;
uint[4] public flag;
function initializeName() public
{
name = ["Sunny","Bob","Lilly"];
}
function initializeFlag() public
{
flag[0] = 21;
flag[1] = 34;
flag[2] = 65;
flag[3] = 12;
}
}
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
2. Dynamic Array
In a dynamic array in Solidity, the size of the array is not mentioned during the declaration.
The size of the dynamic array can be changed during the runtime as more elements are
added to the array.
The below example shows different ways in which the dynamic array can be declared.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract
{
// Dynamic Array
uint[] public flag;
// Dynamic Array with first 3 elements initialized
uint[] public age = [8,15,32];
// Dynamic Array initialized with new keyword
int[] public num = new int[](5);
}
Array Methods in Solidity
The array in Solidity support some methods to manipulate the arrays. They are as follows –
• length
• push()
• push(x)
• pop()
length
This is available for both dynamic storage arrays & fixed size arrays. This is used to get the
length or size of the array.
The below example shows how to use length with fixed size arrays. The output shows the
length of the array which is 4 in our example.
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract
{
// Fixed Size array
uint[4] age = [8,15,32,21];
function getAgeLength() public view returns (uint)
{
return age.length;
}
}
Output –
0:uint256: 4
This example shows the length of the dynamic array whose output is 3 over here.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract
{
// Dynamic Array
uint[] flag = [54,87,14];
function getFlagLength() public view returns (uint)
{
return flag.length;
}
}
Output –
0:uint256: 3
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
push()
This is available for the dynamic storage array and is used to append a zero-initialized element
at the end of the array.In the below example, we have initialized an array called flag and also
declared two state variables flagSize and flagLastValue to keep track of the size of the array
and the value at its last position.A function pushFlag() is created where using the push method
a new zero initialized element is appended at the end of the array. Also, the values of flagSize
and flagLastValue is updated.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract
{ // Dynamic Array
uint[] flag = [54,87,14];
uint public flagSize = flag.length;
uint public flagLastValue = flag[flagSize-1];
function pushFlag() public
{
flag.push();
flagSize = flag.length;
flagLastValue = flag[flagSize-1];
}
}
Before calling the pushFlag() we can see that the value of flagLastValue is 14 and the value
of flagSize is 3.
Output –
0:uint256: 14
0:uint256: 3
After calling the pushFlag() the push() function appends the new element at the end
initialized with zero. The flagSize has now become 4 and flagLastValue of the new last
element is 0.
Output –
0:uint256: 0
0:uint256: 4
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
push(x)
This is available for the dynamic storage array and is used to append the value ‘x’ at the end
of the array.In the below example, we have modified the above example to use push(x) in
pushFlag() function.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract
{
// Dynamic Array
uint[] flag = [54,87,14];
uint public flagSize = flag.length;
uint public flagLastValue = flag[flagSize-1];
function pushFlag(uint x) public
{
flag.push(x);
flagSize = flag.length;
flagLastValue = flag[flagSize-1];
}
}
Before calling the pushFlag() we can see that the value of flagLastValue is 14 and the value
of flagSize is 3.
Output –
0:uint256: 14
0:uint256: 3
We now call pushFlag() with parameter value 99. The new output shows that the value of
flagLastValue becomes 99 and the value of flagSize becomes 4. This confirms that a new
element 99 has been appended to the ‘flag’ array.
Output –
0:uint256: 99
0:uint256: 4
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
pop()
This is available for the dynamic storage array and is used to remove the last element from
the end of the array.
In the below example, a function popFlag() is created that used pop() function to remove the
last element of the ‘flag’ array. After calling the popFlag() the output confirms that the size of
array is reduced to 2 and its last value is 87.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract
{
// Dynamic Array
uint[] flag = [54,87,14];
uint public flagSize = flag.length;
uint public flagLastValue = flag[flagSize-1];
function popFlag() public
{
flag.pop();
flagSize = flag.length;
flagLastValue = flag[flagSize-1];
}
}
Output –
0:uint256: 87
0:uint256: 2
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
Program 1: Fixed Size Array initialized using Constructor
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
Program 2: Fixed Size array initialized using function
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
Program 3: Dynamic array initialized using function:
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
Special Arrays
Bytes Array:A Solidity byte array is a dynamic array that can hold any number of bytes. A byte array
holds all of the bytes together tightly.
Fixed Byte Arrays
Definition
Fixed byte arrays have a predetermined size and are defined as bytes1, bytes2, ..., up to bytes32.
Each bytesN type holds exactly N bytes.
Program 4: Bytes Array
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
Dynamic Byte Arrays
Definition
Dynamic byte arrays, represented as bytes, can change in size and hold a variable amount of data.
Bytes can be declared as a state variable with initial length size as shown in the following code:
bytes localBytes = new bytes(0) ;
This can be also divided into the following two code lines similar to previously discussed arrays:
bytes localBytes ;localBytes= new bytes (10) ;
Bytes can be assigned values directly, as follows:
localBytes = "Ritesh Modi";
Also, values can be pushed into it, as shown in the following code, if it is located at the storage
location:
localBytes.push(byte(10));
Program 5: Dynamic Bytes Array
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
Special Arrays-String Array
String arrays:String arrays are dynamic, like byte arrays, but they have unique constraints. For
example, string does not have an index so it lacks array members such as length, push, and pop. If
three is a need to perform any of these functions, string variables should be converted to bytes first,
and then converted back to strings after the operation
They can be composed of characters within a single or double quote.
Strings can be declared and assigned values directly, as follows:
String name = 'Ritesh Modi" ;
They can be also converted to bytes, as follows:
Bytes byteName = bytes(name) ;
Prof. Ramya.R.B Department of Computer Engineering, APSIT
Semester: - VII Subject: Blockchain AY: 2024-25
Program 5: String Array
Prof. Ramya.R.B Department of Computer Engineering, APSIT