# Assign a Value to Every Element in an Array?

**URL:** <https://discourse.processing.org/t/assign-a-value-to-every-element-in-an-array/6381>\
**Category:** Beginners\
**Created:** [December 6, 2018, 7:12pm UTC](https://discourse.processing.org/t/assign-a-value-to-every-element-in-an-array/6381 "2018-12-06T19:12:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fixinit1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fixinit1/32/2725_2.png) [@Fixinit1](https://discourse.processing.org/u/Fixinit1)\
**Post date:** [December 6, 2018, 7:12pm UTC](https://discourse.processing.org/t/assign-a-value-to-every-element-in-an-array/6381/1 "2018-12-06T19:12:44Z")

</div>

Can someone tell me how I could assign a value to every element in an array with a single line of code? Is it even possible? Right now i have this,  
yposi[0] = 0;  
yposi[1] = 0;  
yposi[2] = 0;  
yposi[3] = 0;  
yposi[4] = 0;

but is it possible to turn all of that into a single line of code which assigns 0 to every element in the array?

Thanks, Alex.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [December 6, 2018, 7:15pm UTC](https://discourse.processing.org/t/assign-a-value-to-every-element-in-an-array/6381/2 "2018-12-06T19:15:47Z")

</div>

It’s actually 2 lines, one for the `import` and one for the actual command:

```auto
import java.util.Arrays;

int[] yposi = new int[5];

Arrays.fill(yposi, 13); // <--- this

```

---

<div class="post-metadata">

**Author:** ![Fixinit1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fixinit1/32/2725_2.png) [@Fixinit1](https://discourse.processing.org/u/Fixinit1)\
**Post date:** [December 6, 2018, 7:21pm UTC](https://discourse.processing.org/t/assign-a-value-to-every-element-in-an-array/6381/3 "2018-12-06T19:21:20Z")

</div>

Thanks, super helpful. This is exactly what I was looking for.

---

<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:** [December 6, 2018, 7:51pm UTC](https://discourse.processing.org/t/assign-a-value-to-every-element-in-an-array/6381/4 "2018-12-06T19:51:58Z")

</div>

> [@hamoid](#):
>
> It’s actually 2 lines, …

```java
final int QTY = 5;
final int[] ys = new int[QTY];

{
  java.util.Arrays.fill(ys, MAX_INT); // 1 statement w/o import
  println(ys);
  exit();
}

```
