Need help deserializing an integer array.
I'm not sure how I should deserialize multi-dimensional arrays in Java. I'm given something like:
Code :
<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 '[', where each one represents a count in its depth. I.e:
type int[][] = [[I
type int = [i
The problem is, how to I create an array of variable depth? Sorry if it isn't clear, I'm really confused by this problem :S
Re: Need help deserializing an integer array.
2 ways:
1. Manually create a larger array as needed, copying over all the old elements and then deleting the old array.
2. Pre-read the data and get a fixed count of the data set you're reading in.
Re: Need help deserializing an integer array.
I thought I may need to do something like that; 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:
Code :
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.
Re: Need help deserializing an integer array.
hmm, I see your point.
Unfortunately, the Only way I can think of to create an x-dimension array is to use reflection, which means no compile-time type checking.
Code Java:
Object array = Array.newInstance(Integer.class, firstDimensionSize, secondDimensionSize, thirdDimensionSize); // first, second, and third dimension size are intengers. You can have as many or few dimensions as you want
Does your input data have to be in this format? If it doesn't, I would recommend changing the file to use the default Serialize and De-serializing mechanism provided by Java for arrays, then use an ObjectOutputStream to write out to the file and an ObjectInputStream to read in from the file.
Re: Need help deserializing an integer array.
Quote:
how do I create an array of variable depth
Not sure what you mean here. You imply that you don't know the number of dimensions until execution time.
If you were able to create this array at execution time, how would your code use/access it?
Perhaps by a method with an arraylist for argument. There would be an entry in the arraylist for each dimension. The underlying data would be in a single dimension array. Somewhere there would need to be the max number of elements for each dimension.
Re: Need help deserializing an integer array.
If I understand the requirements - an array of arrays in which you don't know the size until runtime - it sounds like you want some sort of data structure, for example a linked list, tree, or graph which can then be initialized recursively and structure via references which can be dynamically changed/removed/initialized at runtime.
Re: Need help deserializing an integer array.
You can do this with the Array class. It is part of the reflection package, but it is designed for just this kind of thing, and it does create real arrays (although they're handled as Objects).
Re: Need help deserializing an integer array.
The new thing learned today:
Code :
Object anArray = java.lang.reflect.Array.newInstance(Class.forName("java.lang.String"), new int[4]);
System.out.println("aA=" +anArray); //aA=[[[[Ljava.lang.String;@187aeca\
Re: Need help deserializing an integer array.
Why make it complicated, why not just:
Code :
import java.lang.reflect.Array;
...
Object anArray = Array.newInstance(String.class, 4);
ETA - oh wait, I was forgetting, you need the Class.forName(..) if deserializing a text representation... forget I said that :rolleyes:
Re: Need help deserializing an integer array.
You get different results with that:
Code :
Object anArray2 = java.lang.reflect.Array.newInstance(String.class, 4);
System.out.println("aA2=" +anArray2); //aA2=[Ljava.lang.String;@12dacd1
String[] aA2S = (String[])anArray2;
System.out.println("aA2S.length=" + aA2S.length); //aA2S.length=4
One dimensional with 4 elements
Re: Need help deserializing an integer array.
Quote:
Originally Posted by
Norm
One dimensional with 4 elements
OIC - oops, my bad - I didn't look too closely at the API, but should have noticed the difference in parameters.
It was late, I was tired, what can I say? OTOH maybe I should get more sleep...