3

I currently have a jagged array Class[][][] which I want to serialise as a normal Class[] array, then convert back into a Class[][][] array after deserialization. Is it at all possible to convert between the two both ways? The dimensions are of constant sizes.

4
  • 2
    You'll need to somehow remember the various sizes of the jagged "bits" if you want to be able to reconstruct the original structure. Commented Jul 19, 2012 at 10:47
  • Sure...Its possible. I am not sure the reason you want to do this, would be much easier to Serialize it exactly how you want to store it ( in this case you want to store it as a jagged array ). Commented Jul 19, 2012 at 11:02
  • @Ramhound the implementation of mono I'm using is getting confused when I try and serialise a jagged array, but seems to have no issue with flat arrays Commented Jul 19, 2012 at 11:06
  • Just seen your edit - if the dimensions are of constant sizes, why not use a [,,] instead? Commented Jul 19, 2012 at 11:37

3 Answers 3

5

This is how you can flatten to a 1-dimensional structure:

var jagged = new object[][][]; var flattened = jagged.SelectMany(inner => inner.SelectMany(innerInner => innerInner)).ToArray(); 

As for going back to multidimensional - this will depend entirely on what it is your trying to achieve/what the data represents

Sign up to request clarification or add additional context in comments.

2 Comments

I think => innerInner.SelectMany() needs to be just => innerInner.
+1 for the mention on "going back", and how it completely depends.
3

If you don't mind serializing a flattened array and an array of ints, you can use the following:

public static int[] JaggedSizes<T>(this T[][][] topArray) { List<int> rtn = new List<int>(); rtn.Add(topArray.Length); for (int i = 0; i < topArray.Length; i++) { var midArray = topArray[i]; rtn.Add(midArray.Length); for (int j = 0; j < midArray.Length; j++) { var botArray = midArray[j]; rtn.Add(botArray.Length); } } return rtn.ToArray(); } // Thanks @Dave Bish public static T[] ToFlat<T>(this T[][][] jagged) { return jagged.SelectMany(inner => inner.SelectMany(innerInner => innerInner)).ToArray(); } public static T[][][] FromFlatWithSizes<T>(this T[] flat, int[] sizes) { int inPtr = 0; int sPtr = 0; int topSize = sizes[sPtr++]; T[][][] rtn = new T[topSize][][]; for (int i = 0; i < topSize; i++) { int midSize = sizes[sPtr++]; T[][] mid = new T[midSize][]; rtn[i] = mid; for (int j = 0; j < midSize; j++) { int botSize = sizes[sPtr++]; T[] bot = new T[botSize]; mid[j] = bot; for (int k = 0; k < botSize; k++) { bot[k] = flat[inPtr++]; } } } return rtn; } 

Comments

2

I don't think so Rory.

You may have been able to do this if it is a Class[,,] multidimensional array, but the fact that each array could be of different length is going to always be a stumbling block.

Assuming you serialize if as a Class[] + another class to give you the original dimensions, you'll be golden.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.