# Dynamic Arrays

Recall in Data Structures that we can declare a static array as follows:

```solidity
T[] arrayName = new T[](n);
```

where `T` is the type of the array. The good news is that defining a dynamic array is not that much different; below is the syntax for declaring a dynamic array:

```
T[] arrayName;
```

Declaring a dynamic array seems simple, but what about defining a dynamic array? Below is the syntax to define a dynamic array:

```solidity
T[] arrayName = [] // Insert array elements here
```

Since *dynamic arrays* can vary in size, we would ideally like to have functions available to us which allow us to grow/shrink our array. And indeed, Solidity provides us with such functions.&#x20;

* `array.push(x)`: pushes `x` to the back of `array`
* `array.pop()`: pops the last element of `array`

And like with static arrays, we can also index into dynamic arrays; it should be noted, however, that this can cause runtime errors if the user is attempting to index into an undefined location of a dynamic array.

{% hint style="info" %}

#### Dynamic Arrays in Memory?

As with mappings, dynamic arrays are reserved only for state variables; one cannot declare/define a new dynamic array variable within a function.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cs4998.cornellblockchain.org/introduction/hello-world-pt.-2/dynamic-arrays-and-strings/dynamic-arrays.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
