This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes dynamic Array (ArrayList) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dynamic Array (ArrayList)" Watch "dynamic Array (ArrayList)" New topic
Author

dynamic Array (ArrayList)

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I needed a dynamic array so I did an ArrayList. My actual method needs to return a int array. SO in order to do that do I have to declare an in array of the size of my ArrayList and then get each element out convert it to an int and then put it into my int array or is there a way I can do it in one shot w/o a loop?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
Anthony -
You can get part way there by calling the toArray( ) method, that'll get you an array of Objects...


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Or, assuming your ArrayList contains Integer objects, you can get a tiny bit further with

This way you have an Integer[] rather than an Object[]. But still, if what you really wanted was an int[], you need a loop. And as long as you have to do that anyway, you might as well forget the toArray() stuff and build the array you want directly:

It would be nice if there were a one-liner for this, but it's not that big a deal really...


"I'm not back." - Bill Harding, Twister
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: dynamic Array (ArrayList)
 
Similar Threads
how to create array using linked list....
Passing dynamic values in the value attribute of ITERATOR
arrays in java
How do you remove a String [ ] from an ArrayList?
What is causing this NoSuchElementException?