How can I quickly up-cast object[,] into double[,]?
object[,] src = new object[2, 3];
// Initialize src with test doubles.
src[0, 0] = 1.0;
src[0, 1] = 2.0;
src[0, 2] = 3.0;
src[1, 0] = 4.0;
src[1, 1] = 5.0;
src[1, 2] = 6.0;
double[,] dst = new double[src.GetLength(0), src.GetLength(1)];
Array.Copy(src, dst, src.Length);
Java correct way convert/cast object to Double
You can't cast an object to a Double
if the object is not a Double.
Check out the API.
particularly note
valueOf(double d);
and
valueOf(String s);
Those methods give you a way of getting a Double
instance from a String or double primitive. (Also not the constructors; read the documentation to see how they work) The object you are trying to convert naturally has to give you something that can be transformed into a double.
Finally, keep in mind that Double
instances are immutable -- once created you can't change them.
Fastest Way for Converting an Object to Double?
You must be doing a whole whopping lot of these in order to make any sense to spend any time on this. However, I am not here to judge:
So, your code is this:
if (o is IConvertible)
{
d = ((IConvertible)o).ToDouble(null);
}
else
{
d = 0d;
}
I wonder if you would be better off with this
IConvertible convert = o as IConvertible;
if (convert != null)
{
d = convert.ToDouble(null);
}
else
{
d = 0d;
}
Saves you the double cast.
Java: many ways of casting a (long) Object to double
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 Want to Convert object[,] to double[,] in C#
You can't cast, since double may have a different size in memory than an object reference, thus the size of the array would also be different.
C# does not have many helpers for multidimensional arrays, also LINQ mostly doesn't work here. You'll have to convert it yourself:
double[,] array = new double[temp.GetLength(0),temp.GetLength(1)];
for(int x = 0; x < array.GetLength(0); x++)
for(int y = y; y < array.GetLength(1); y++)
array[x,y] = (double)temp[x,y];
C# also provides a static Array.ConvertAll method, but this seems to lack overloads for multidimensional arrays.
EDIT: The second one has to be GetLength(1), not (0).
Cast Object to Double[]
If you want to cast Object[]
to Double[]
knowing that each your Object
in your array is actually a Double
, use Arrays.copyOf
method. Otherwise it's impossible to do a "direct" cast, you must check your items type and figure out how to extract a double
value for that.
Maybe more optimal will be using Arrays.asList
method and cast it to List<Double>
directly. (because Arrays.asList
doesn't copy the array)
Example:
static void test(Object[] a) {
Double[] dummy = Arrays.copyOf(a, a.length, Double[].class);
System.out.println(dummy[0]); // prints 42.0
}
public static void main (String[] args)
{
Object[] a = new Object[1];
a[0] = new Double(42.0);
test(a);
}
Example with generic List casting:
static void test(Object[] a) {
List<Double> dummy = (List<Double>)((List<?>)(Arrays.asList(a)));
System.out.println(dummy.get(0));
}
Converting Object to Double in Java
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);
converting Object [] to double [] in java
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.
Double casting
Yeah, I'm pretty sure that's not a thing. There's no reason that double casting would ever be necessary - it's possible it might get rid of a compile warning about unsafe casting (in which case you're probably doing it wrong), but otherwise that's just not right.
I mean, there's auto toString
calling e.g. println("" + i)
, but even then you don't need to cast to an object first...
Edit: After reading Tom's answer I'm suddenly unsure about this answer - primitives and (particularly) generics can actually use this. I don't have the ability to test anything right now, but anyone reading this answer should definitely take a look at his (and probably upvote it).
I'm going to stick to the line that there are no (or at least extremely few and far between) good reasons to do this, however, and the provided example certainly has nothing to do with it.
Related Topics
C# Array.Findallindexof Which Findall Indexof
Is There Any Difference Between Datetime in C# and Datetime in SQL Server
What Are C# Lambda's Compiled Into? a Stackframe, an Instance of an Anonymous Type, Or
Error Making Azure Management Library API Call When Authenticating with Azure Active Directory
Entity Framework - Add Navigation Property Manually
How to Create a Friendly Url in ASP.NET MVC
Update a Record Without First Querying
How to Add Parameters into a Webrequest
Use Dependency Injection in Static Class
Ef4 Code First: How to Add a Relationship Without Adding a Navigation Property
C# Creating an Unknown Generic Type at Runtime
How to Group by on 2 Child Entities and Get Total of Both This Child Entities