• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Type double[] array to Number[] array

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello -

I have a double[] array of the form,



which should rather be in the form of a Number[] array - i.e.,



How would I go about casting or converting an array of double into an array of Number? Note,



this works but I am assuming that there may be a less cumbersome way to do so.
 
Saloon Keeper
Posts: 6930
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like the for() loop better but you could do this
 
Marshal
Posts: 68095
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you instantiate Number like that? It is abstract.
 
Ranch Hand
Posts: 145
2
Mac Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The for loop works. With autboxing you could also use a for-each loop which might read a bit nicer, but that is just preference.

You can't assign a double[] to a Number[] because they are both distinct objects and the only thing they both inherit from is Object.
 
Sheriff
Posts: 21866
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a stream:
Note that there is no Collector that directly returns an array, so a List as intermediate is required.
 
Campbell Ritchie
Marshal
Posts: 68095
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about this?Since it says here that the type of the returned array is the runtime type of the argument, does it matter what size the array passed is? Can you pass
new Number[]{null}
as an argument instead?
Both would not require any instances in the array, getting us round the problem of Number being abstract.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also pass new Number[0].
 
Rob Spoor
Sheriff
Posts: 21866
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can, but then two Number arrays are created if you have more than 1 double in the original array. I always try to prevent that by, if it's known, passing the size of the collection I'm trying to convert to an array.
 
Campbell Ritchie
Marshal
Posts: 68095
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Rob Spoor
Sheriff
Posts: 21866
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!