This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Any good method to use when converting Integer List to int[]? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Any good method to use when converting Integer List to int[]?" Watch "Any good method to use when converting Integer List to int[]?" New topic
Author

Any good method to use when converting Integer List to int[]?

Mingwei Jiang
Ranch Hand

Joined: Feb 19, 2004
Posts: 63
The thing is like this: I have a method which has int[] as its return value (the method signature cannot be changed), but in the implementation of this method, I found very hard to define the length of this return value until the end. So I used List to contain return values first and thinking I could extract this list to the int[] and return. But I didn't find any existing method for doing so, I mean converting Integer List to int[]. What I did now is write a separate method to interate over Integer list and construct the int[] to return.

Is there any convenient method there for me to use? Because I feel a little embarrassed to write this conversion like code.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
There are converience methods on the List itself, but they deal with objects. e.g.

List<Integer> results = new ArrayList<Integer>();
Integer [] values = results.toArray(new Integer[] {});
[ May 14, 2007: Message edited by: John Bartlett ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If you have an array of Integer objects, obtained as shown by previous poster, then auto-boxing will allow you to treat the elements as if they were primitive ints.

However, if you really need an array of primitive ints, perhaps because you are going to pass it to a function that requires one, then I think you have no option but to write a little loop.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Mingwei Jiang
Ranch Hand

Joined: Feb 19, 2004
Posts: 63
Yeah, I really need those primitive ints, seems I have no choice but write a little loop.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

The Apache Jakarta Commons Lang utility library contains a method to do that. Example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Any good method to use when converting Integer List to int[]?
 
Similar Threads
Prevent a method from being inherited- from the SUB class
compareTo method
Reading a String backward
question on reflection
Need suggestion on implementation