Saving/Storing a multidimensional array

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 !

1 Like

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

You can just do this to save the Data :

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 :

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]);
  }
}
2 Likes
/**
 * 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. :wink:

5 Likes

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 !

1 Like

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

/**
 * 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;
}
3 Likes