# Can I fill an array by using a for loop?

**URL:** <https://discourse.processing.org/t/can-i-fill-an-array-by-using-a-for-loop/39330>\
**Category:** Beginners\
**Created:** [October 17, 2022, 11:34pm UTC](https://discourse.processing.org/t/can-i-fill-an-array-by-using-a-for-loop/39330 "2022-10-17T23:34:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExplodingWhale](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@ExplodingWhale](https://discourse.processing.org/u/ExplodingWhale)\
**Post date:** [October 17, 2022, 11:34pm UTC](https://discourse.processing.org/t/can-i-fill-an-array-by-using-a-for-loop/39330/1 "2022-10-17T23:34:07Z")

</div>

If i want to store values of integers from 0 to 1000 with increments of 20, what would be the easiest way to put those values into the array? And what about a 2D-array?

there has to be an easier way than this, right?:  
int numbers = {0,20,40,60,80,100,120,140,etc,etc};

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [October 18, 2022, 12:22am UTC](https://discourse.processing.org/t/can-i-fill-an-array-by-using-a-for-loop/39330/2 "2022-10-18T00:22:02Z")

</div>

Hi Sir

Filling array Method’s

> **[Arrays](https://processing.org/tutorials/arrays/)**
>
> How to store and access data in array structures.

> **[Array / Examples](https://processing.org/examples/array.html)**
>
> An array is a list of data. Each piece of data in an array is identified by an index number representing its position in the array. Arrays are zero based, which means that the first element in the arr…

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 18, 2022, 12:23am UTC](https://discourse.processing.org/t/can-i-fill-an-array-by-using-a-for-loop/39330/3 "2022-10-18T00:23:55Z")

</div>

> [@ExplodingWhale](#):
>
> There has to be an easier way than this, right?

This is my attempt. Not sure if it’d work for any range though:

```auto
static final int MIN = 0, MAX = 1000, STEP = 20;
static final int LEN = (MAX - MIN) / STEP + 1;

final int[] nums = new int[LEN];

void setup() {
  for (int i = 0; i < LEN; nums[i] = i++ * STEP + MIN);
  println(nums);
  exit();
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [October 18, 2022, 12:47am UTC](https://discourse.processing.org/t/can-i-fill-an-array-by-using-a-for-loop/39330/4 "2022-10-18T00:47:03Z")

</div>

Hi  
One More reference

> **[Array 2D / Examples](https://processing.org/examples/array2d.html)**
>
> Demonstrates the syntax for creating a two-dimensional (2D) array. Values in a 2D array are accessed through two index values. 2D arrays are useful for storing images. In this example, each dot is col…
