Topic RSS18:28, EEST
May 29, 2018
OfflineI subscribe an Array on a Server.
If i became onDataChange listener an Array. I can printout this, but how can i used it as Array in java to get .size or getIndex() and so on?
This is how i do:
System.out.println(machineData.getValue());
}
And the output:
My Datatyp of value ist Variant
thx đŸ™‚
14:08, EEST
April 3, 2012
OfflineHi,
This somewhat goes into general java programming category, which is generally outside of support. However as this can be sometimes a bit confusing:
Typically you know the actual type of the value that is encoded to the Variant and can just cast the value (Variant.getValue()) to the correct type, e.g. if you know the type is Double, then you can just
Variant variant = …
Double v = (Double) variant.getValue();
Similarly if you know it is singledimensional array of Doubles you can do
Double[] v = (Double[]) variant.getValue();
If it 1-dim array of not known type you can use Object[].
It should be noted that the value can be more than one-dimensional array. Please read the UA specification Part 6, section 5.2.2.16 “Variant” for more information.
15:32, EEST
December 21, 2011
Offline4:19, EET
January 27, 2020
OfflineBjarne Boström said
Hi,This somewhat goes into general java programming category, which is generally outside of support. However as this can be sometimes a bit confusing:
Typically you know the actual type of the value that is encoded to the Variant and can just cast the value (Variant.getValue()) to the correct type, e.g. if you know the type is Double, then you can just
Variant variant = …
Double v = (Double) variant.getValue();Similarly if you know it is singledimensional array of Doubles you can do
Double[] v = (Double[]) variant.getValue();
If it 1-dim array of not known type you can use Object[].
It should be noted that the value can be more than one-dimensional array. Please read the UA specification Part 6, section 5.2.2.16 “Variant” for more information. Â
The conversion method for arrays outlined in this post doesn’t work for me. I’m forced to do an element-by element conversion. Are there any utility methods in the client SDK that I can use instead?
11:48, EET
April 3, 2012
Offline16:51, EET
January 30, 2020
OfflineIn our code we’re transforming the array into a Variant[]. This way, you can convert each item in the array the way you like.
if (value.isArray()) {
Object[] array = (Object[]) value.getValue();
return Arrays.stream(array).map((o) -> Variant::new).toArray(Variant[]::new);
} else {
// Value is not an array, handle as you wish
}
}
1 Guest(s)

Log In
Register