Is it possible in CodenameOne to save multidimensional arrays?

Viewed 46

I know that I can only save a small amount of classes using Storage in CodenameOne but I'm wondering whether it is possible to save multi dimensional arrays.

1 Answers

There is no detection of multi-dimensional arrays but you can save them using manual code e.g. in your Externalizable interface you can use something such as this for writing:

 if(myMultiArr == null) {
     out.writeInt(0);
 } else {
     out.writeInt(myMultiArr.length);
     for(int iter = 0 ; iter < myMultiArr.length ; iter++) {
         if(myMultiArr[iter] == null) {
             out.writeInt(0);
         } else {
             out.writeInt(myMultiArr[iter].length);
             for(int i = 0 ; i < myMultiArr[iter].length ; i++) {
                 out.writeInt(myMultiArr[iter][i]);
             }
         }
     }
 }

Then you can read it with:

 myMultiArr = new int[in.readInt()][];
 for(int iter = 0 ; iter < myMultiArr.length ; iter++) {
      myMultiArr[iter] = new int[in.readInt()];
      for(int i = 0 ; i < myMultiArr[iter].length ; i++) {
          myMultiArr[iter][i] = in.readInt();
      }
 }

Notice this code will convert null values to 0 length arrays and assumes a multi dimensional array of integers. It's pretty easy to adapt it to anything though.

Related