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.)
问题是,我需要反序列化这样的东西(每个节点都有其他属性,比如对象SUID,为了简单起见我已经排除了这些属性。)
<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:
每个节点上的“object”属性描述它是什么对象。如果它是一个基本类型的数组,它将以[括号数组]开头,其中每个括号代表其深度的计数。即:
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:
问题是我不知道我是如何以编程方式创建一个深度为x的数组?即,如果我必须按照我的方式去做,那可能是这样的:
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
必须有更好的方法:S
Another problem arises when I need to unbox this array and make it a mutlidimensional array of a primitive type.
当我需要取消打开这个数组并使其成为基本类型的多维数组时,会出现另一个问题。
2 个解决方案
#1
0
ArrayList is not an array. Generics have type erasure at runtime so what you are trying to with the switch is meaningless at runtime.
ArrayList不是数组。泛型在运行时具有类型擦除功能,因此您在交换机上尝试使用该功能时无意义。
If you want to create a List just create it without generics.
如果您想创建一个List,只需创建它而不用泛型。
List list = new ArraysList();
If you want to create an array of a specific type you can use Arrays.
如果要创建特定类型的数组,可以使用数组。
Object array = Arrays.newArray(arrayType, arrayDepth);
I would rethink your file format. Its verbose for the amount of data you are conveying.
我会重新考虑你的文件格式。它传达的数据量很大。
how about something like
怎么样的
int[[ 2
1,2
3,4
This conveys the same information in a much more compact format.
这以更紧凑的格式传达相同的信息。
#2
0
You can create an array whose type and dimensionality are not known at compile time using Array.newInstance(Class, int...)
. For example, the following create an 3 x 4 x 5 array of int
.
您可以使用Array.newInstance(Class,int ...)创建一个在编译时未知类型和维度的数组。例如,以下创建一个3 x 4 x 5的int数组。
Object array = Array.instance(Integer.CLASS, 3, 4, 5);
or
Object array = Array.instance(Integer.CLASS, new int[]{3, 4, 5});
This solves the second part of your question as well.
这也解决了问题的第二部分。
#1
0
ArrayList is not an array. Generics have type erasure at runtime so what you are trying to with the switch is meaningless at runtime.
ArrayList不是数组。泛型在运行时具有类型擦除功能,因此您在交换机上尝试使用该功能时无意义。
If you want to create a List just create it without generics.
如果您想创建一个List,只需创建它而不用泛型。
List list = new ArraysList();
If you want to create an array of a specific type you can use Arrays.
如果要创建特定类型的数组,可以使用数组。
Object array = Arrays.newArray(arrayType, arrayDepth);
I would rethink your file format. Its verbose for the amount of data you are conveying.
我会重新考虑你的文件格式。它传达的数据量很大。
how about something like
怎么样的
int[[ 2
1,2
3,4
This conveys the same information in a much more compact format.
这以更紧凑的格式传达相同的信息。
#2
0
You can create an array whose type and dimensionality are not known at compile time using Array.newInstance(Class, int...)
. For example, the following create an 3 x 4 x 5 array of int
.
您可以使用Array.newInstance(Class,int ...)创建一个在编译时未知类型和维度的数组。例如,以下创建一个3 x 4 x 5的int数组。
Object array = Array.instance(Integer.CLASS, 3, 4, 5);
or
Object array = Array.instance(Integer.CLASS, new int[]{3, 4, 5});
This solves the second part of your question as well.
这也解决了问题的第二部分。