如何在 java 中保留 double 的二维数组
how do I persist 2 dimensional array of double in java
我正在增强桌面 java 应用程序,该应用程序使用 XSD
和 java xjc.exe
编译器来保存和恢复对象数据。我需要保留一个包含 double[][]
特征数据的新类型。我补充说:
<xs:schema
...
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<xs:import namespace="http://schemas.xmlsoap.org/soap/encoding/"
schemaLocation="http://schemas.xmlsoap.org/soap/encoding/"/>
<xs:complexType name="patchFeature">
<xs:all>
<xs:element name="featureMatrix" type="doubleMatrix" />
</xs:all>
<xs:attribute name="type" type="patchType" use="required" />
</xs:complexType>
<xs:complexType name="doubleMatrix">
<xs:complexContent>
<xs:restriction base="soapenc:Array">
<xs:attribute ref="soapenc:arrayType"
wsdl:arrayType="xs:double[][]"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
...
</xs:schema>
到 XSD
文件。 xjc.exe
编译器可以编译它,但是它生成的 PatchFeature
和 DoubleMatrix
类 没有 get/set 和 double[][]
值的方法。如何保留原始双精度的二维数组?我不想使用 XSD
序列,因为它们生成的对象采用 List<Double>
,这需要装箱、拆箱和转换 to/from 原始 double[][]
矩阵。
我终于放弃了尝试 persist/restore double[][] 数组。相反,我坚持每行一个 'stride' 个元素和一个一维 ArrayList。我在 double[][] 数组和 stride/ArrayList 之间编写了转换器。它不是很优雅,但它确实有效。 xsl 看起来像:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://novodynamics.com/NovoHealth/features.xsd"
xmlns="http://novodynamics.com/NovoHealth/features.xsd"
elementFormDefault="qualified">
<xs:complexType name="patchFeature">
<xs:all>
<xs:element name="featureVector" type="doubleVector" />
</xs:all>
<xs:attribute name="stride" type="xs:int" use="required" />
</xs:complexType>
<xs:complexType name="doubleVector">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="dbl" type="xs:double" />
</xs:sequence>
</xs:complexType>
...
以及转换器:
private double[][] featureMatrix;
public void setFeatureMatrix(int stride, List<Double> vector) {
this.featureMatrix = new double[vector.size() / stride][stride];
for (int i = 0; i < vector.size(); i++) {
this.featureMatrix[i / stride][i % stride] = vector.get(i);
}
}
public static void createPatchFeature(double[][] featureMatrix, Feature feature,
ObjectFactory factory) {
PatchFeature patchFeature = factory.createPatchFeature();
int stride = featureMatrix[0].length;
// int vectorSize = stride * featureMatrix.length;
patchFeature.setStride(stride);
DoubleVector vector = factory.createDoubleVector();
for (int i = 0; i < featureMatrix.length; i++) {
for (int j = 0; j < stride; j++) {
vector.getDbl().add(featureMatrix[i][j]);
}
}
patchFeature.setFeatureVector(vector);
feature.setPatch(patchFeature);
}
我正在增强桌面 java 应用程序,该应用程序使用 XSD
和 java xjc.exe
编译器来保存和恢复对象数据。我需要保留一个包含 double[][]
特征数据的新类型。我补充说:
<xs:schema
...
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<xs:import namespace="http://schemas.xmlsoap.org/soap/encoding/"
schemaLocation="http://schemas.xmlsoap.org/soap/encoding/"/>
<xs:complexType name="patchFeature">
<xs:all>
<xs:element name="featureMatrix" type="doubleMatrix" />
</xs:all>
<xs:attribute name="type" type="patchType" use="required" />
</xs:complexType>
<xs:complexType name="doubleMatrix">
<xs:complexContent>
<xs:restriction base="soapenc:Array">
<xs:attribute ref="soapenc:arrayType"
wsdl:arrayType="xs:double[][]"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
...
</xs:schema>
到 XSD
文件。 xjc.exe
编译器可以编译它,但是它生成的 PatchFeature
和 DoubleMatrix
类 没有 get/set 和 double[][]
值的方法。如何保留原始双精度的二维数组?我不想使用 XSD
序列,因为它们生成的对象采用 List<Double>
,这需要装箱、拆箱和转换 to/from 原始 double[][]
矩阵。
我终于放弃了尝试 persist/restore double[][] 数组。相反,我坚持每行一个 'stride' 个元素和一个一维 ArrayList。我在 double[][] 数组和 stride/ArrayList 之间编写了转换器。它不是很优雅,但它确实有效。 xsl 看起来像:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://novodynamics.com/NovoHealth/features.xsd"
xmlns="http://novodynamics.com/NovoHealth/features.xsd"
elementFormDefault="qualified">
<xs:complexType name="patchFeature">
<xs:all>
<xs:element name="featureVector" type="doubleVector" />
</xs:all>
<xs:attribute name="stride" type="xs:int" use="required" />
</xs:complexType>
<xs:complexType name="doubleVector">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="dbl" type="xs:double" />
</xs:sequence>
</xs:complexType>
...
以及转换器:
private double[][] featureMatrix;
public void setFeatureMatrix(int stride, List<Double> vector) {
this.featureMatrix = new double[vector.size() / stride][stride];
for (int i = 0; i < vector.size(); i++) {
this.featureMatrix[i / stride][i % stride] = vector.get(i);
}
}
public static void createPatchFeature(double[][] featureMatrix, Feature feature,
ObjectFactory factory) {
PatchFeature patchFeature = factory.createPatchFeature();
int stride = featureMatrix[0].length;
// int vectorSize = stride * featureMatrix.length;
patchFeature.setStride(stride);
DoubleVector vector = factory.createDoubleVector();
for (int i = 0; i < featureMatrix.length; i++) {
for (int j = 0; j < stride; j++) {
vector.getDbl().add(featureMatrix[i][j]);
}
}
patchFeature.setFeatureVector(vector);
feature.setPatch(patchFeature);
}