469,336 Members | 5,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Casting values from Object array

In our code we load data into Vector variables. However, in using a third party API we are required to send it the data in array form, for example as an array of double values (double x[][]).

I tried the following code and failed to compile:
Expand|Select|Wrap|Line Numbers
  1.  
  2. // In load routine
  3. double[][] data = new double[2][];
  4. data[0] = (double [])mParameters[0].getMData().toArray();
  5.  
  6.  
The getMData() method returns a java.util.Vector object.

Any suggestions?
Thanks!
Oct 6 '08 #1
10 11607
I'm pretty sure you have to specify all of the dimensions of an array when initializing. So try:

Expand|Select|Wrap|Line Numbers
  1. double[][] data = new double[2][/*some integer*/];
Oct 6 '08 #2
I'm pretty sure you have to specify all of the dimensions of an array when initializing. So try:

Expand|Select|Wrap|Line Numbers
  1. double[][] data = new double[2][/*some integer*/];
I'm fairly new at Java so I am not 100% sure but this line is not the problem on my code. According to my sources the second dimension on the array does not need to be specified when the variable is instantiated. Thanks for the advice though :).
Oct 6 '08 #3
Ganon11
3,652 Expert 2GB
Could we see exactly what the compilation error is?
Oct 6 '08 #4
Could we see exactly what the compilation error is?
I apologize for not posting the error earlier:

Expand|Select|Wrap|Line Numbers
  1. ParameterObject.java:45: inconvertible types
  2. found   : java.lang.Object[]
  3. required: double[]
  4.         double [] x = (double [])mData.toArray();
  5. Note: TextParser.java uses unchecked or unsafe operations.
  6. Note: Recompile with -Xlint:unchecked for details.
  7. 1 error
  8. BUILD FAILED (total time: 0 seconds)
  9.  
As an update, the code in this post looks a bit different because I have been working and trying new things to get it to work but the concept/issue remains the same: how to convert an Object[] to a double[].
Oct 6 '08 #5
Ganon11
3,652 Expert 2GB
And the Vector contains doubles or Doubles? You may have to change data to be a Double[][] instead of a double[][], or put in a few extra steps of evaluation.
Oct 6 '08 #6
And the Vector contains doubles or Doubles? You may have to change data to be a Double[][] instead of a double[][], or put in a few extra steps of evaluation.

From what I have read, Object types can not be cast to primitives. So the only way I have found to make my code work is to convert each value in the array one by one:

Expand|Select|Wrap|Line Numbers
  1. double[] d = new double[mData.size()];
  2. for(int i = 0; i < mData.size(); ++i)
  3. {
  4.     d[i] = Double.parseDouble(mData.get(i).toString());
  5. }
  6.  
I will try the Double type instead of the primitive double to see what I get. Mostly I wish (if possible) to avoid the overhead of the toString() call.

P.S. the mData vector contains doubles not Doubles.
Oct 7 '08 #7
From what I have read, Object types can not be cast to primitives. So the only way I have found to make my code work is to convert each value in the array one by one:

Expand|Select|Wrap|Line Numbers
  1. double[] d = new double[mData.size()];
  2. for(int i = 0; i < mData.size(); ++i)
  3. {
  4.     d[i] = Double.parseDouble(mData.get(i).toString());
  5. }
  6.  
I will try the Double type instead of the primitive double to see what I get. Mostly I wish (if possible) to avoid the overhead of the toString() call.

P.S. the mData vector contains doubles not Doubles.

From the NetBeans debugger, this is the message from an exception thrown from the code shown below:

"[Ljava.lang.Object; cannot be cast to [Ljava.lang.Double;"

Expand|Select|Wrap|Line Numbers
  1. // Throws above exception
  2. public Double[] getArrayData()
  3. {
  4.    return((Double[]) mData.toArray());
  5. }
  6.  
Oct 7 '08 #8
JosAH
11,448 Expert 8TB
As you already have noticed you can't cast to and fro primitives and Objects of
any kind. If you have a List or Vector or array of Objects and you want to cast
them to a primitive type all those Objects have to be a wrapper class of a (numerical)
primitive type (Double, Long, Integer or whatever).

This is how you cast a List of Objects to an array of doubles:

Expand|Select|Wrap|Line Numbers
  1. public double[] toArray(List<Object> list) {
  2.    double[] array= new array(list.size());
  3.    for (int i= 0; i < array.length; i++)
  4.       array[i]= ((Double)list.get(i)).doubleValue();
  5.    return array;
  6. }
  7.  
kind regards,

Jos
Oct 7 '08 #9
As you already have noticed you can't cast to and fro primitives and Objects of
any kind. If you have a List or Vector or array of Objects and you want to cast
them to a primitive type all those Objects have to be a wrapper class of a (numerical)
primitive type (Double, Long, Integer or whatever).

This is how you cast a List of Objects to an array of doubles:

Expand|Select|Wrap|Line Numbers
  1. public double[] toArray(List<Object> list) {
  2.    double[] array= new array(list.size());
  3.    for (int i= 0; i < array.length; i++)
  4.       array[i]= ((Double)list.get(i)).doubleValue();
  5.    return array;
  6. }
  7.  
kind regards,

Jos
Thanks for the reply. This is very much in step with what I thought as far as having to convert each value at a time. It works like a charm.

Thanks!
Oct 8 '08 #10
JosAH
11,448 Expert 8TB
Thanks for the reply. This is very much in step with what I thought as far as having to convert each value at a time. It works like a charm.

Thanks!
I goofed in line #2 though; it should've read:

Expand|Select|Wrap|Line Numbers
  1. double[] array= new double[list.size()];
  2.  
kind regards,

Jos
Oct 8 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Karl Meissner | last post: by
2 posts views Thread by Zac | last post: by
3 posts views Thread by Steve Teeples | last post: by
3 posts views Thread by thomson | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.