new Double(object.toString());
But it seems weird to me that you're going from an Object to a Double. You should have a better idea what class of object you're starting with before attempting a conversion. You might have a bit of a code quality problem there.
Note that this is a conversion, not casting.
Answer from RichW on Stack Overflownew Double(object.toString());
But it seems weird to me that you're going from an Object to a Double. You should have a better idea what class of object you're starting with before attempting a conversion. You might have a bit of a code quality problem there.
Note that this is a conversion, not casting.
If your Object represents a number, eg, such as an Integer, you can cast it to a Number then call the doubleValue() method.
Double asDouble(Object o) {
Double val = null;
if (o instanceof Number) {
val = ((Number) o).doubleValue();
}
return val;
}
The non-generic toArray might not be optimal, I'd recommend you to use a for loop instead:
Long[] v = new Long[entry.getValue().singleValues.size()];
int i = 0;
for(Long v : entry.getValue().singleValues) {
v[i++] = v;
}
Now you've got an array of Long objects instead of Object. However, Long is an integral value rather than floating-point. You should be able to cast, but it smells like an underlying problem.
You can also convert directly instead of using a Long array:
double[] v = new double[entry.getValue().singleValues.size()];
int i = 0;
for(Long v : entry.getValue().singleValues) {
v[i++] = v.doubleValue();
}
Concept: you must not try to convert the array here, but instead convert each element and store the results in a new array.
In order to "convert" an array of type Object[] to double[] you need to create a new double[] and populate it with values of type double, which you'll get by casting each Object from the input array separately, presumably in a loop.
As every primitive number in Java gets cast to its boxing type when an object is needed (in our case Long) and every boxed number is an instance of Number the safest way for doing so is:
final Object object = 0xdeadbeefL;
final double d = ((Number)object).doubleValue();
The danger here is, as always, that the Object we want to cast is not of type Number in which case you will get a ClassCastException. You may check the type of the object like
if(object instanceof Number) ...
if you like to prevent class cast exceptions and instead supply a default value like 0.0. Also silently failing methods are not always a good idea.
I have an
Object objthat I know is actually along.
No, you don't. long is a primitive data type, and primitive types in Java are not objects. Note that there's a difference between the primitive type long and java.lang.Long, which is a wrapper class.
You cannot cast a Long (object) to a long (primitive). To get the long value out of a Long, call longValue() on it:
Long obj = ...;
long value = obj.longValue();
Is it safe to directly cast it to
double?
If it's actually a primitive long, then yes, you can cast that to a double. If it's a Long object, you don't need to cast, you can just call doubleValue() on it:
double x = obj.doubleValue();
Instead of
temp =(String)rows.get(j).getAtIndex(i); //java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
result[j]=Double.parseDouble(temp);
just use the following:
result[j]=(Double)rows.get(j).getAtIndex(i);
To be somewhat more bulletproof I would combine the answers by esej and vizier... (but only if this wasn't a performance bottle neck!)
Object temp = rows.get(j).getAtIndex(i);
if (temp instanceof Double) {
result[j]=(Double)rows.get(j).getAtIndex(i);
} else {
try {
temp = String.valueOf(rows.get(j).getAtIndex(i));
result[j]= Double.parseDouble(temp);
} catch (NumberFormatException e) {
// logging and recovery code goes here, or rethrow as an exception you can handle.
}
}
If this is the performance bottleneck of the application then by all means go with what vizier said :)
If you don't mind using a 3rd party library, commons-lang has the ArrayUtils type with various methods for manipulation.
Double[] doubles;
...
double[] d = ArrayUtils.toPrimitive(doubles);
There is also the complementary method
doubles = ArrayUtils.toObject(d);
Edit: To answer the rest of the question. There will be some overhead to doing this, but unless the array is really big you shouldn't worry about it. Test it first to see if it is a problem before refactoring.
Implementing the method you had actually asked about would give something like this.
double[] getDoubles(int columnIndex) {
return ArrayUtils.toPrimitive(data[columnIndex]);
}
In Java 8, this is one-liner:
Double[] boxed = new Double[] { 1.0, 2.0, 3.0 };
double[] unboxed = Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();
Note that this still iterates over the original array and creates a new one.