Results 1 to 7 of 7
- 04-27-2012, 12:17 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Convert Object[] to Double[] using Arrays.copyOfRange
Hi all,
I have an array: Object[] row that looks like this:
[1SWTGKWR, 36N32, A, AR, 15, 0, 29, 30, 12, 86, 34.09, 65.91]
I want to convert it to an Double[] row2 like this:
[15, 0, 29, 30, 12, 86, 34.09, 65.91]
In essence, I want to create a second array of double values that consist of row[4] through row[11].
I'm initialize double[] row2 thusly:
In attempts to accomplish this, I'm doingJava Code:Double[] row2 = new Double[7];
I'm receiving the following error:Java Code:row2 = Arrays.copyOfRange(row, 4, row2.length, Double[].class);
Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.Arrays.copyOfRange(Unknown Source)
at PCQ.ChartPanel.getRow(ChartPanel.java:121)
at PCQ.ChartPanel$1.actionPerformed(ChartPanel.java:9 2)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mou seReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMo useEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Where have I gone wrong?
-
Re: Convert Object[] to Double[] using Arrays.copyOfRange
Have you checked out the API for ArrayStoreException? It states:
Perhaps you want to iterate through the array in a for loop and copy the values over to the second array. I'm not sure if you can Arrays.copyOfRange here, but let's see what smarter minds think.Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.
By the way, what sort of animal is 1SWTGKWR ? It's not a String, not a variable, ... what is it?
Also, if you iterate through your array and have each item print out what type it is, what do you get? I have a feeling that those numbers may not be numbers.Last edited by Fubarable; 04-27-2012 at 12:44 AM.
- 04-27-2012, 01:04 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: Convert Object[] to Double[] using Arrays.copyOfRange
Solved. Java is getting more intuitive as the months pass. I've only been at it since January :p Thanks Furry ;)
Edit: I receive the following when I try to up your reputation: "You must spread some Reputation around before giving it to Fubarable again."Java Code:double[] row2 = new double[8]; int index = 0; for(int i = 4; i < 12; i++){ Object object = row[i]; String string = object.toString(); double dub = Double.valueOf(string).doubleValue(); row2[index] = dub; ++index; }Last edited by Redefine12; 04-27-2012 at 05:56 PM. Reason: posted Double[], meant double[]
-
Re: Convert Object[] to Double[] using Arrays.copyOfRange
- 04-27-2012, 04:27 AM #5
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Convert Object[] to Double[] using Arrays.copyOfRange
If you stores the double values as java.lang.Double in your object array you can just casting it directly into double. No need to convert it to string and then create a double value out of that string.
Java Code:Object[] objects = new Object[] { 10d, 20d }; Double[] doubles = new Double[objects.length]; doubles[0] = (Double) objects[0]; doubles[1] = (Double) objects[1];Website: Learn Java by Examples
- 04-27-2012, 07:23 AM #6
- 04-27-2012, 05:43 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: Convert Object[] to Double[] using Arrays.copyOfRange
Similar Threads
-
convert integer to double
By himanwish in forum New To JavaReplies: 4Last Post: 03-17-2011, 01:55 AM -
Convert from string to double
By Lord ice in forum New To JavaReplies: 4Last Post: 12-12-2010, 05:27 PM -
How to convert a double into a int?
By tyang in forum New To JavaReplies: 4Last Post: 02-10-2010, 10:02 AM -
convert String to Double
By azurovyhrosik in forum CLDC and MIDPReplies: 5Last Post: 10-22-2008, 02:46 AM -
convert string to a double?
By javaMike in forum Advanced JavaReplies: 2Last Post: 11-27-2007, 03:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks