# How to set default value of a 2D array?

**URL:** <https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948>\
**Category:** Beginners\
**Created:** [February 20, 2021, 8:01am UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948 "2021-02-20T08:01:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [February 20, 2021, 8:01am UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948/1 "2021-02-20T08:01:46Z")

</div>

Hi!  
When I used to use 2D fixed arrays (int[][], or int[] for that matter) it was always a bit annoying setting the values. Like default boolean state in an array is false. Is it possible to set it to something else without using

```auto
int w = 10, h = 10, defaultValue = 123;
int grid[][] = new int[w][h];
for(int i = 0;i < w; i++) for(int j = 0; j < h; j++) grid[i][j] = defaultValue;

```

---

<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:** [February 20, 2021, 8:56am UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948/2 "2021-02-20T08:56:20Z")

</div>

> [@CodeMasterX](#):
>
> Is it possible to set it to something else without using

Nope! If it’s primitive type default value is `0` and `false` for `boolean`.  
For non-primitive types default value is `null`.

> **[Primitive Data Types (The Java™ Tutorials \>        
            Learning...](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)**
>
> This beginner Java tutorial describes fundamentals of programming in the Java programming language

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 23, 2021, 10:10am UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948/3 "2021-02-23T10:10:34Z")

</div>

```auto

// int w = 10, h = 10,
final int defaultValue = 123;

int grid[][] = {
  { defaultValue, defaultValue, defaultValue, defaultValue, defaultValue}, 
  { defaultValue, defaultValue, defaultValue, defaultValue, defaultValue}, 
  { defaultValue, defaultValue, defaultValue, defaultValue, defaultValue}, 
  { defaultValue, defaultValue, defaultValue, defaultValue, defaultValue}, 
  { defaultValue, defaultValue, defaultValue, defaultValue, defaultValue}, 
  { defaultValue, defaultValue, defaultValue, defaultValue, defaultValue}
};

size(660, 660); 

for (int i = 0; i < grid.length; i++) {
  for (int j = 0; j < grid[i].length; j++) {
    text(grid[i][j], 
      i*39+40, j*18+40);
  }
}

```

😉

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [June 29, 2021, 12:12pm UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948/4 "2021-06-29T12:12:45Z")

</div>

is there really no such thing as `boolean abc[] = new boolean[9](true)`?  
except for `={true,true,true,true,true,...}`

---

<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:** [June 29, 2021, 12:39pm UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948/5 "2021-06-29T12:39:46Z")

</div>

Well, most we can do in Java is use Arrays.**fill()**:  
[docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#fill(byte[],byte)](http://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#fill(byte%5B%5D,byte))

```auto
final boolean[] abc = new boolean[9];
java.util.Arrays.fill(abc, true);

println(str(abc));
exit();

```

For your previous 2D array we can shorten its initialization loop using Arrays.**fill()** this way:

```auto
static final int ROWS = 3, COLS = 2, DEFAULT = 123;
final int[][] grid = new int[ROWS][COLS];

{
  for (final int[] row : grid) java.util.Arrays.fill(row, DEFAULT);
}

void setup() {
  for (final int[] row : grid) println(str(row));
  exit();
}

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [June 29, 2021, 1:06pm UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948/6 "2021-06-29T13:06:01Z")

</div>

Hi!  
Thank you for showing me this!

I just went through several java documentation pages and saw a “0x123a” example. It is supposed to be hex number. Is there any other 0x value or something like 2x010101?

---

<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:** [June 29, 2021, 1:08pm UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948/7 "2021-06-29T13:08:41Z")

</div>

Use prefix `0b` for binary literals: `static final int DEFAULT = 0b010101; // 21`

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [June 29, 2021, 1:10pm UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948/8 "2021-06-29T13:10:16Z")

</div>

are there any others? would 0d100 mean 100 in decimal?

---

<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:** [June 29, 2021, 1:18pm UTC](https://discourse.processing.org/t/how-to-set-default-value-of-a-2d-array/27948/9 "2021-06-29T13:18:15Z")

</div>

Look for section “Integer Literals” for all available Java prefixes on my 1st posted link:  
[docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html](http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

However it seems like they’ve skipped the octal number system.  
For octal literals just prefix them w/ `0` like this: `0400` = `256`.
