Casting values from Object array 
October 6th, 2008, 05:09 PM
| | Newbie | | Join Date: Mar 2008
Posts: 26
| |
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: -
-
// In load routine
-
double[][] data = new double[2][];
-
data[0] = (double [])mParameters[0].getMData().toArray();
-
-
The getMData() method returns a java.util.Vector object.
Any suggestions?
Thanks!
| 
October 6th, 2008, 05:57 PM
| | Member | | Join Date: Sep 2008
Posts: 40
| | | re: Casting values from Object array
I'm pretty sure you have to specify all of the dimensions of an array when initializing. So try: - double[][] data = new double[2][/*some integer*/];
| 
October 6th, 2008, 06:23 PM
| | Newbie | | Join Date: Mar 2008
Posts: 26
| | | re: Casting values from Object array Quote: |
Originally Posted by labmonkey111 I'm pretty sure you have to specify all of the dimensions of an array when initializing. So try: - 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 :).
| 
October 6th, 2008, 07:39 PM
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,444
| | | re: Casting values from Object array
Could we see exactly what the compilation error is?
| 
October 6th, 2008, 09:16 PM
| | Newbie | | Join Date: Mar 2008
Posts: 26
| | | re: Casting values from Object array Quote: |
Originally Posted by Ganon11 Could we see exactly what the compilation error is? | I apologize for not posting the error earlier: -
ParameterObject.java:45: inconvertible types
-
found : java.lang.Object[]
-
required: double[]
-
double [] x = (double [])mData.toArray();
-
Note: TextParser.java uses unchecked or unsafe operations.
-
Note: Recompile with -Xlint:unchecked for details.
-
1 error
-
BUILD FAILED (total time: 0 seconds)
-
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[].
| 
October 6th, 2008, 10:52 PM
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,444
| | | re: Casting values from Object array
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.
| 
October 7th, 2008, 03:03 PM
| | Newbie | | Join Date: Mar 2008
Posts: 26
| | | re: Casting values from Object array Quote: |
Originally Posted by Ganon11 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: -
double[] d = new double[mData.size()];
-
for(int i = 0; i < mData.size(); ++i)
-
{
-
d[i] = Double.parseDouble(mData.get(i).toString());
-
}
-
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.
| 
October 7th, 2008, 07:44 PM
| | Newbie | | Join Date: Mar 2008
Posts: 26
| | | re: Casting values from Object array Quote: |
Originally Posted by meLlamanJefe 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: -
double[] d = new double[mData.size()];
-
for(int i = 0; i < mData.size(); ++i)
-
{
-
d[i] = Double.parseDouble(mData.get(i).toString());
-
}
-
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;" -
// Throws above exception
-
public Double[] getArrayData()
-
{
-
return((Double[]) mData.toArray());
-
}
-
| 
October 7th, 2008, 08:10 PM
|  | Expert | | Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2 | | | re: Casting values from Object array
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: -
public double[] toArray(List<Object> list) {
-
double[] array= new array(list.size());
-
for (int i= 0; i < array.length; i++)
-
array[i]= ((Double)list.get(i)).doubleValue();
-
return array;
-
}
-
kind regards,
Jos
| 
October 8th, 2008, 03:00 PM
| | Newbie | | Join Date: Mar 2008
Posts: 26
| | | re: Casting values from Object array Quote: |
Originally Posted by JosAH 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: -
public double[] toArray(List<Object> list) {
-
double[] array= new array(list.size());
-
for (int i= 0; i < array.length; i++)
-
array[i]= ((Double)list.get(i)).doubleValue();
-
return array;
-
}
-
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!
| 
October 8th, 2008, 04:28 PM
|  | Expert | | Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2 | | | re: Casting values from Object array Quote: |
Originally Posted by meLlamanJefe 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: -
double[] array= new double[list.size()];
-
kind regards,
Jos
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,702 network members.
|