I’m writing a module which serializes others and I’ve got everything working only I’m unsure of how I should deserialize multidimensional arrays.
The problem is, I need to deserialize something like this(There are other attributes for each node, such as the objects SUID, that I’ve excluded for simplicity’s sake.)
<var object="[[I">
<_0 object="[I">
<_0 object="java.lang.Integer">1</_0>
<_1 object="java.lang.Integer">2</_1>
</_0>
<_1 object="[I">
<_0 object="java.lang.Integer">3</_0>
<_1 object="java.lang.Integer">4</_1>
</_1>
</var>
Where the “object” attribute on each node describes what object it is. If it is an array of a primitive type, it will begin with an array of [ brackets, where each one represents a count in its depth. I.e:
type int[][] = [[I
type int[] = [i
The problem is I don’t know how I’d programatically create an array with a depth of x? I.e, if I had to do it my way it would probably be something like:
switch(iArrayDepth)
{
case 1:
return new ArrayList<Integer>();
case 2:
return new ArrayList<ArrayList<Integer>>();
case 3:
return new ArrayList<ArrayList<ArrayList<Integer>>>();
etc...
}
There must be a better way of doing it :S
Another problem arises when I need to unbox this array and make it a mutlidimensional array of a primitive type.
ArrayList is not an array. Generics have type erasure at runtime so what you are trying to with the switch is meaningless at runtime.
If you want to create a List just create it without generics.
If you want to create an array of a specific type you can use Arrays.
I would rethink your file format. Its verbose for the amount of data you are conveying.
how about something like
This conveys the same information in a much more compact format.