• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting into primitive wrapper class

 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there,
playing with the Collection Framework, I was wondering about the following issue:
What is the most efficient way (or: _any_ way but a loop) to cast a array of primitives
int myArray[] = {1,2,3,4,5}
into a array of Integer wrapper objects?
My goal was to be able to handle myArray[] as a Collection.
Arrays.asList(Object[] a)
looked like a appropriate method to me. But, as already quoted, I had to cast it manually to a help-array
Integer myHelpArray[]
which worked out, but doesn't look too efficient to me ;-)
thanks for any help,
Jan
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Jan!
We beginners aren't too interested in alleged efficiencies, especially as premature optimization is the root of all evil [Knuth]. We're more concerned with clarity of code.
Moving this to the Performance forum...
[ February 03, 2004: Message edited by: Dirk Schreckmann ]
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Dirk,
thanks for answering so fast!
As english is not my mother tongue, it seems to me that I put my question a bit too much into the "hey, how can I get the last edge of performance out of my code?"-corner.
Considering myself as a beginner with java, let me try to put my intention behind the question in other words:
I have an array of primitves. I'd love to transform it into a Collection, to do all the fancy collection-stuff with it.
The only method I found which vaguely does what I want is "Arrays.asList(Object[] a)".
My first question: Is this the only way? Is this okay or does it look too complicated or so? Are there other (better?) methods?
When I'm using this method, I need an array of Objects, not of primitives. The only way I found to get this is to cast element per element from "int" to "Integer".
My second question: Is looping through the whole array the only way? -> If I do so I could also fill a Collection with my array's elements...
From my practise in other languages and my impression of Java I feel that there might be a simple one-liner of code, recieving my array of primitives and returning a object of a class that implements Collection.
If you say "no, the way is definitely okay", I'll be more then happy. Otherwise, just let me know how you would have solved this "problem"
;-)
Greetings from Berlin, Germany
Jan
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No there is no one-liner to change an array of primitives to an array of wrapper class objects.
Bill
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way would be ( in pseudo code ):
Create List
For Each Element In Primitive Array
Convert Element To Wrapped Element
Add Wrapped Element To List
does that help ?
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for answering.
So I summarize:
- There is no one-step way to get a array of primitives into a collection
- The way I tried to do it was okay

Thanks again !
bye,
Jan
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello There,

I need the opposite -- a "long []" from a "Long []".

A method needs to return a long [] from a DB/SQL query. Inside the method, we create an ArrayList<Long>. Now what is an easy way to convert this ArrayList to a "long []" ?

I could NOT find anything in jakarta commons/ClassUtils.

Thanks for any pointers.

BR,
~A
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no better way than to do the obvious:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic