简体   繁体   中英

Cast Object to Double[]

I have a method which takes a parameter of type Object.

protected Object foo(Object data) {
    Double[] values = (Double[]) data;    //Line-A
    return Math.sqrt(Math.pow(values[0] - values[3], 2)
            + Math.pow(values[1] - values[4], 2)
            + Math.pow(values[2] - values[5], 2));
}

At the runtime, this function throws an exception, at Line-A:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Double;

How can I get this function to work?

Edit [Tuesday, April 1, 2014]

Can we please assume the following?

  1. The API is totally out of my control.
  2. The parameter coming in to this function ( data ) is of type Object[] , and I cannot change its type.

Given these assumptions, how can I get this function to work?

I have a method which takes a parameter of type Object

Why? This is poor API design. If you expect a Double[], specify a Double[].

At the runtime, this function throws an exception, at Line-A:

Because the argument you passed wasn't a Double[].

How can I get this function to work?

Pass a Double[] to it as the argument.

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));
}

Why do you need Object parameter ?? You are expecting Double[] so use Double[]. Having object is useful when you really don't know what is coming out (generic type perhaps?), and for a more complex design anyway. Using Object in this case is pointless and bad API design

So just do this

protected Double foo(Double[] data) {
    Double[] values = data;    //Line-A
    return Math.sqrt(Math.pow(values[0] - values[3], 2)
            + Math.pow(values[1] - values[4], 2)
            + Math.pow(values[2] - values[5], 2));
}

This is how I did it.

protected Object foo(Object data) {
    Double[] values = new Double[6];  
    Object[] objectArr = (Object[]) data;
    int index = 0;
    for (Object object : objectArr) {
        values[index] = (Double)object;
        index++;
    }
    return Math.sqrt(Math.pow(values[0] - values[3], 2)
            + Math.pow(values[1] - values[4], 2)
            + Math.pow(values[2] - values[5], 2));
}

Please provide an answer if you have a better way to do this, or leave a comment if you have any concerns!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM