0
0
Blockchain / Solidityprogramming~3 mins

Why Arrays (fixed and dynamic) in Blockchain / Solidity? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could organize endless transactions without rewriting your entire list every time?

The Scenario

Imagine you are trying to keep track of a list of transactions on a piece of paper. Every time a new transaction happens, you have to erase and rewrite the entire list to add the new one. This is like managing data manually without a proper structure.

The Problem

Writing down each transaction manually is slow and easy to mess up. You might forget to update the list correctly or run out of space on your paper. It's hard to find a specific transaction quickly, and changing the list means rewriting everything.

The Solution

Arrays let you store many items in one place, like a neat row of boxes. Fixed arrays have a set size, so you know exactly how many items fit. Dynamic arrays can grow or shrink as needed, making it easy to add or remove transactions without rewriting everything.

Before vs After
Before
transactionList = "";
transactionList += "tx1";
transactionList += ", tx2";
transactionList += ", tx3";
After
transactions = new DynamicArray();
transactions.add("tx1");
transactions.add("tx2");
transactions.add("tx3");
What It Enables

Arrays make it simple to organize, access, and update collections of data efficiently, even as the data changes over time.

Real Life Example

In blockchain, arrays are used to store lists of transactions or blocks. Dynamic arrays help add new blocks as the chain grows, while fixed arrays can store a set number of validators or participants.

Key Takeaways

Manual data lists are slow and error-prone.

Fixed arrays have a set size; dynamic arrays can change size.

Arrays help manage collections of data easily and efficiently.