18:28, EEST
May 29, 2018
I 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
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.
15:32, EEST
December 21, 2011
4:19, EET
January 27, 2020
Bjarne 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
16:51, EET
January 30, 2020
In 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
}
}
Most Users Ever Online: 1919
Currently Online:
13 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Heikki Tahvanainen: 402
hbrackel: 144
rocket science: 88
pramanj: 86
Francesco Zambon: 83
Ibrahim: 78
Sabari: 62
kapsl: 57
gjevremovic: 49
Xavier: 43
Member Stats:
Guest Posters: 0
Members: 736
Moderators: 7
Admins: 1
Forum Stats:
Groups: 3
Forums: 15
Topics: 1524
Posts: 6450
Newest Members:
kristiewinkle8, rust, christamcdowall, redaahern07571, nigelbdhmp, travistimmons, AnnelCib, dalenegettinger, howardkennerley, ThomassnismModerators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1026, Jimmy Ni: 26, Matti Siponen: 346, Lusetti: 0
Administrators: admin: 1