How do i convert Long[] to long[]? For example i know how to convert if it is not array as below.
long ll = (Long)Object[1].longValue()
But how do i convert for an array, the below is not right but can anyone correct it?
long[] l_ar = (Long[])Object[]
Java int can be converted to long in two simple ways: This is known as implicit type casting or type promotion, the compiler automatically converts smaller data types to larger data types. Using valueOf() method of the Long wrapper class in java which converts int to long.
In Java Long is an object and like any object it can be null. long is a primitive type and cannot be null.
You could steal a solution based on ArrayUtils
Long[] longObjects = { 1L, 2L, 3L }; long[] longArray = ArrayUtils.toPrimitive(longObjects);
There are no standard API method for doing that (how would null
-elements be handled?) so you would need to create such a method yourself.
Something like this (will throw NullPointerException
on any object beeing null
):
public static long[] toPrimitives(Long... objects) { long[] primitives = new long[objects.length]; for (int i = 0; i < objects.length; i++) primitives[i] = objects[i]; return primitives; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With