Connecting Tech Pros Worldwide Help | Site Map

Casting values from Object array

  #1  
Old 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:
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!
  #2  
Old 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:

Expand|Select|Wrap|Line Numbers
  1. double[][] data = new double[2][/*some integer*/];
  #3  
Old 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:

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 :).
  #4  
Old October 6th, 2008, 07:39 PM
Ganon11's Avatar
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?
  #5  
Old 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:

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[].
  #6  
Old October 6th, 2008, 10:52 PM
Ganon11's Avatar
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.
  #7  
Old 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:

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.
  #8  
Old 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:

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.  
  #9  
Old October 7th, 2008, 08:10 PM
JosAH's Avatar
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:

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
  #10  
Old 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:

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!
  #11  
Old October 8th, 2008, 04:28 PM
JosAH's Avatar
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:

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

Jos
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
for vs foreach for array enumeration garyusenet@myway.com answers 9 November 29th, 2006 05:55 PM
Casting arrays fails at runtime eric.dennison@gmail.com answers 2 June 8th, 2006 07:25 PM
Web Method returns array of custom type : casting error John Grandy answers 7 November 23rd, 2005 05:52 AM
Casting struct[] to object[] Fabrizio answers 4 November 16th, 2005 07:08 AM