473,375 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,375 software developers and data experts.

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 11888
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

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

Similar topics

7
by: Brian P | last post by:
I am getting an invalid cast exception when I try to take an ArrayList with datetime values and use the ToArray method to create an object array. I need to use an object array becase I'm working...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
2
by: Karl Meissner | last post by:
I am having problem casting from object. I want to pass in an value type as an object and then store it as a particular type. The cast from object throws an exception if the types do not match. ...
2
by: Zac | last post by:
Alright anyone who has 2c throw it in... I am working through a custom xml serializer and have come upon a conundrum, given our class design. The interface implemented on the base class (base...
3
by: Steve Teeples | last post by:
I have a method that passes in two string objects (both numerical numbers) and a string identifying their type. public bool DoCompare(string num1, string num2, string theirType) { System.Type...
3
by: thomson | last post by:
Hi all, Can any one tell me what happens when an object is casted to an interface, somthing bit deeper, the case is i do have an interface Ifunction which has got one method display(), and i...
2
by: jay1_z | last post by:
Here is my code in VB: Return Me.Document.GetItemValue("Subject")(0) The "GetItemValue" method returns an 'object' array, and the first item in the array is a string. This works for me in VB...
1
by: comp.lang.php | last post by:
array_walk($result, create_function('$a, $b', 'return (strtolower($a->{$section . "_name"}) < strtolower($b->{$section . "_name"}));'), $section); I thought this would sort an object array by...
4
by: buzzluck68 | last post by:
Hello, I am having trouble upgrading my VB apps to .net. When i started at my company there was a dll created here in vb6, and i need to use that dll for many functions that would take too much...
3
by: nady | last post by:
Dear Techies, I have a code which retrieves values from database cmdSelect = New OleDbCommand("select fields,finesFields from trafficReport where checked LIKE 1", conn) cmdSelectDr =...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.