# Saving/Storing a multidimensional array

**URL:** <https://discourse.processing.org/t/saving-storing-a-multidimensional-array/16391>\
**Category:** Coding Questions\
**Created:** [December 14, 2019, 11:07am UTC](https://discourse.processing.org/t/saving-storing-a-multidimensional-array/16391 "2019-12-14T11:07:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baz](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Baz](https://discourse.processing.org/u/Baz)\
**Post date:** [December 14, 2019, 11:07am UTC](https://discourse.processing.org/t/saving-storing-a-multidimensional-array/16391/1 "2019-12-14T11:07:03Z")

</div>

For a project of mine I want to create a function that can save a 3d double or a 2d double array such as I can recover what was inside if I restart the program. I think you can do that in JSON, but I never used it, and I didn’t find something understandable for me.  
How would you do that ? Do you think JSON can be used, or is there other methods that could work ? Thanks in advance !

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 14, 2019, 11:49am UTC](https://discourse.processing.org/t/saving-storing-a-multidimensional-array/16391/2 "2019-12-14T11:49:12Z")

</div>

By far The simplest method would be to save it as a txt file…

You can just do this to save the Data :

```auto
String[] lines = new String[arr.length];

for (int i = 0; i < lines.length; i++) {
  for (int j = 0; j < lines[i].length; j++) {
    lines[i] += arr[i][j] + ",";
  }
}
saveStrings("Data.txt", lines);

```

And to load it :

```auto
String[] lines = loadStrings("Data.txt");

for (int i = 0; i < lines.length; i++) {
  String[] splits = split(lines[i], ',');
  for (int j = 0; j < splits.length; j++) {
    arr[i][j] = Double.parseDouble(splits[j]);
  }
}

```

---

<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 14, 2019, 6:55pm UTC](https://discourse.processing.org/t/saving-storing-a-multidimensional-array/16391/3 "2019-12-14T18:55:31Z")

</div>

> [@Baz](#):
>
> Do you think JSON can be used, or is there other methods that could work?

- 1st of all, you’re gonna need a function which converts your multidimensional array to a [JSONArray](https://processing.org/reference/JSONArray.html) container.
- Inside a multi-loop, use method **append()** to add more [JSONArray](https://processing.org/reference/JSONArray.html) containers, and finally the values themselves inside the innermost dimension:  
[append() / Reference / Processing.org](http://Processing.org/reference/JSONArray_append_.html)
- Then you can use **saveJSONArray()** to save it as a JSON file:  
[saveJSONArray() / Reference / Processing.org](http://Processing.org/reference/saveJSONArray_.html)

```java
/**
 * Conversion Arr3d to JSONArr3d (v0.1)
 * GoToLoop (2019/Dec/14)
 * Discourse.Processing.org/t/saving-storing-a-multidimensional-array/16391/3
 */

static final String FILENAME = "JArr3D.json", OPT = "compact";

double[][][] my3dArr = new double[4][2][3];
JSONArray myJArr;

void setup() {
  myJArr = arr3dToJArr(my3dArr);
  println(myJArr);
  saveJSONArray(myJArr, dataPath(FILENAME), OPT);
  exit();
}

static final JSONArray arr3dToJArr(final double[][][] arr3d) {
  final JSONArray jarr3d = new JSONArray();

  for (final double[][] arr2d : arr3d) {
    final JSONArray jarr2d = new JSONArray();
    jarr3d.append(jarr2d);

    for (final double[] arr1d : arr2d) {
      final JSONArray jarr1d = new JSONArray();
      jarr2d.append(jarr1d);

      for (final double d : arr1d) jarr1d.append(d);
    }
  }

  return jarr3d;
}

```

I did the Arr3d to JArray part. You just need to do the reverse. 😉

---

<div class="post-metadata">

**Author:** ![Baz](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Baz](https://discourse.processing.org/u/Baz)\
**Post date:** [December 15, 2019, 2:51pm UTC](https://discourse.processing.org/t/saving-storing-a-multidimensional-array/16391/4 "2019-12-15T14:51:30Z")

</div>

After a bit of struggle I managed to make this work. I’m new to JSON but it wasn’t actually that hard. Thanks a lot !

---

<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 18, 2019, 12:31am UTC](https://discourse.processing.org/t/saving-storing-a-multidimensional-array/16391/5 "2019-12-18T00:31:54Z")

</div>

For completeness’ sake I’m posting the “finalized” version which adds the loading part: 👼

```auto
/**
 * Conversion Arr3d to JSONArr3d (v1.0)
 * GoToLoop (2019/Dec/17)
 * Discourse.Processing.org/t/saving-storing-a-multidimensional-array/16391/5
 */

static final String FILENAME = "JArr3D.json", OPT = "compact";
static final int RANGE = 100, DIMS = 4;

double[][][] my3dArr = generateArr3d(RANGE, DIMS);
JSONArray my3dJarr;

void setup() {
  my3dJarr = arr3dToJarr3d(my3dArr);
  println(my3dJarr.format(-1));
  saveJSONArray(my3dJarr, dataPath(FILENAME), OPT);

  my3dJarr = loadJSONArray(FILENAME);
  my3dArr = jarr3dToArr3d(my3dJarr);
  println(joinArr3d(my3dArr));

  exit();
}

static final JSONArray arr3dToJarr3d(final double[][][] arr3d) {
  final JSONArray jarr3d = new JSONArray();

  for (final double[][] arr2d : arr3d) {
    final JSONArray jarr2d = new JSONArray();
    jarr3d.append(jarr2d);

    for (final double[] arr1d : arr2d) {
      final JSONArray jarr1d = new JSONArray();
      jarr2d.append(jarr1d);

      for (final double d : arr1d) jarr1d.append(d);
    }
  }

  return jarr3d;
}

static final double[][][] jarr3dToArr3d(final JSONArray jarr3d) {
  final int lenZ = jarr3d.size();
  final double[][][] arr3d = new double[lenZ][][];

  for (int z = 0; z < lenZ; ++z) {
    final JSONArray jarr2d = jarr3d.getJSONArray(z);
    final int lenY = jarr2d.size();
    final double[][] arr2d = arr3d[z] = new double[lenY][];

    for (int y = 0; y < lenY; ++y)
      arr2d[y] = jarr2d.getJSONArray(y).getDoubleArray();
  }

  return arr3d;
}

static final String joinArr3d(final double[][][] arr3d) {
  final StringBuilder sb = new StringBuilder();
  int idxZ = 0;

  for (final double[][] arr2d : arr3d) {
    int idxY = 0;
    sb.append(ENTER);

    for (final double[] arr1d : arr2d) {
      int idxX = 0;

      for (final double d : arr1d) sb
        .append('[').append(idxZ).append("][")
        .append(idxY).append("][").append(idxX++)
        .append("] ").append(d).append(ENTER);

      ++idxY;
    }

    ++idxZ;
  }

  return sb.toString();
}

double[][][] generateArr3d(final double valRange, final int dimsRange) {
  return generateArr3d(valRange, dimsRange, dimsRange, dimsRange);
}

double[][][] generateArr3d(
  final double valRange, 
  final int depthRange, 
  final int heightRange, 
  final int widthRange) {

  final int d = (int) random(1, depthRange + 1);
  final int h = (int) random(1, heightRange + 1);
  final int w = (int) random(1, widthRange + 1);

  final double[][][] arr3d = new double[d][h][w];
  println(d, h, w);

  for (final double[][] arr2d : arr3d) for (final double[] arr1d : arr2d)
    for (int i = 0; i < w; arr1d[i++] = Math.random() * valRange);

  return arr3d;
}

```
