#100DaysOfSolidity 📚 Array: An Introduction to Solidity Language #13

#100DaysOfSolidity Series 013 “Array:”

Solidity Academy
4 min readJul 2, 2023

Welcome to the 13th post of the #100DaysOfSolidity series! In this article, we will explore the concept of arrays in Solidity, a statically-typed programming language used for developing smart contracts on the Ethereum blockchain. Arrays are an essential data structure in programming, allowing us to store and manipulate collections of elements. Solidity provides various ways to work with arrays, including initializing, accessing, modifying, and removing elements. We’ll cover these aspects in detail and provide code samples to demonstrate their usage.

#100DaysOfSolidity 📚 Array: An Introduction to Solidity Language #13

🧱 Solidity Array Initialization:

In Solidity, arrays can have a compile-time fixed size or a dynamic size. Let’s start by exploring different ways to initialize an array:

1️⃣ Dynamic-sized Array:

```solidity
uint[] public arr;
uint[] public arr2 = [1, 2, 3];
```

In the first example, we declare a dynamic-sized array named `arr` without specifying an initial size. The `public` keyword makes the array accessible from outside the contract. In the second example, `arr2` is initialized with three elements: 1, 2, and 3.

2️⃣ Fixed-sized Array:

```solidity
uint[10] public myFixedSizeArr;
```

Here, we declare a fixed-sized array named `myFixedSizeArr` with a size of 10. Fixed-sized arrays have a predetermined length that cannot be changed during runtime.

🔍 Array Access and Modification:

Solidity provides several functions and techniques to access and modify array elements. Let’s explore some of them:

1️⃣ Accessing Elements:

```solidity
function get(uint i) public view returns (uint) {
return arr[i];
}
```

The `get` function allows us to retrieve the value at a specific index `i` from the `arr` array.

2️⃣ Returning the Entire Array:

```solidity
function getArr() public view returns (uint[] memory) {
return arr;
}
```

The `getArr` function returns the entire `arr` array. However, be cautious when using this approach with arrays that can grow indefinitely, as it can consume a significant amount of gas.

3️⃣ Appending Elements:

```solidity
function push(uint i) public {
arr.push(i);
}
```

The `push` function appends a new element `i` to the end of the `arr` array, increasing its length by one.

4️⃣ Removing Elements:

```solidity
function pop() public {
arr.pop();
}
```

The `pop` function removes the last element from the `arr` array, decreasing its length by one.

5️⃣ Array Length:

```solidity
function getLength() public view returns (uint) {
return arr.length;
}
```

The `getLength` function returns the current length of the `arr` array.

6️⃣ Deleting Elements:

```solidity
function remove(uint index) public {
delete arr[index];
}
```

The `remove` function deletes the element at the specified index `index` in the `arr` array. The `delete` keyword resets the value at that index to its default value (0 in this case) without changing the array length.

7️⃣ Working with Memory Arrays:

```solidity
function examples() external {
uint[] memory a = new uint[](5);
}
```

In the `examples` function, we demonstrate how to create a memory array with a fixed size of 5. Memory arrays are useful when you need to create temporary arrays within a function.

🔀 Array Removal Techniques:

Now let’s explore two different techniques for removing elements from an array in Solidity:

1️⃣ Remove by Shifting Elements:

contract ArrayRemoveByShifting {
uint[] public arr;
function remove(uint _index) public {
require(_index < arr.length, "index out of bound");
for (uint i = _index; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr.pop();
}
function test() external {
arr = [1, 2, 3, 4, 5];
remove(2);
// [1, 2, 4, 5]
assert(arr[0] == 1);
assert(arr[1] == 2);
assert(arr[2] == 4);
assert(arr[3] == 5);
assert(arr.length == 4);
arr = [1];
remove(0);
// []
assert(arr.length == 0);
}
}

In this example, the `remove` function shifts elements from right to left starting from the specified index `_index`. It then uses the `pop` function to remove the duplicated last element.

2️⃣ Remove by Replacing with Last Element:

contract ArrayReplaceFromEnd {
uint[] public arr;
function remove(uint index) public {
arr[index] = arr[arr.length - 1];
arr.pop();
}
function test() public {
arr = [1, 2, 3, 4];
remove(1);
// [1, 4, 3]
assert(arr.length == 3);
assert(arr[0] == 1);
assert(arr[1] == 4);
assert(arr[2] == 3);
remove(2);
// [1, 4]
assert(arr.length == 2);
assert(arr[0] == 1);
assert(arr[1] == 4);
}
}

In this example, the `remove` function replaces the element at the specified index `index` with the last element in the array. It then uses the `pop` function to remove the duplicated last element, effectively deleting the desired element.

📝 Conclusion:

In this article, we’ve explored the concept of arrays in Solidity. We’ve covered array initialization, access, modification, and removal techniques using code samples. Arrays are powerful data structures that allow us to work with collections of elements efficiently. It’s essential to choose the appropriate array type and consider gas costs when working with arrays that can grow indefinitely. Understanding arrays in Solidity is crucial for building complex smart contracts and dApps on the Ethereum blockchain.

🔗 Additional Resources:

Thank you for reading! 🙏🚀

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/