Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old October 6th, 2008, 05:09 PM
Newbie
 
Join Date: Mar 2008
Posts: 16
Default 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!
Reply
  #2  
Old October 6th, 2008, 05:57 PM
Newbie
 
Join Date: Sep 2008
Posts: 31
Default

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*/];
Reply
  #3  
Old October 6th, 2008, 06:23 PM
Newbie
 
Join Date: Mar 2008
Posts: 16
Default

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 :).
Reply
  #4  
Old October 6th, 2008, 07:39 PM
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Age: 19
Posts: 3,426
Default

Could we see exactly what the compilation error is?
Reply
  #5  
Old October 6th, 2008, 09:16 PM
Newbie
 
Join Date: Mar 2008
Posts: 16
Default

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[].
Reply
  #6  
Old October 6th, 2008, 10:52 PM
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Age: 19
Posts: 3,426
Default

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.
Reply
  #7  
Old October 7th, 2008, 03:03 PM
Newbie
 
Join Date: Mar 2008
Posts: 16
Default

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.
Reply
  #8  
Old October 7th, 2008, 07:44 PM
Newbie
 
Join Date: Mar 2008
Posts: 16
Default

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.  
Reply
  #9  
Old October 7th, 2008, 08:10 PM
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 8,480
Default

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
Reply
  #10  
Old October 8th, 2008, 03:00 PM
Newbie
 
Join Date: Mar 2008
Posts: 16
Default

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!
Reply
  #11  
Old October 8th, 2008, 04:28 PM
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 8,480
Default

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
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles