Results 1 to 4 of 4
- 01-01-2011, 04:34 PM #1
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
turning an ArrayList into an Array
When constructing a list of data, an ArrayList is usefull, but when using that data, an array is more convenient. My question is, what is the best way to turn an arrayList of type1 into an array of type1?
I usually do it like this:
However this is an O(n) operation and not very neat. Is there a better way?Java Code:Object[] O=exampleArrayListOfType1.toArray(); type1[] T=new type1[O.length]; for(int i=0; i<O.length; i++) { T[i]=(type1) O[i]; }
- 01-01-2011, 04:47 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
- 01-01-2011, 04:52 PM #3
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
As the ArrayList is backed by an array, it could technicly support an O(1) turning into an array, but destroying the ArrayList in the proces. But I can understand why they didn't implement that.
- 01-01-2011, 04:58 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Array vs ArrayList
By Lund01 in forum New To JavaReplies: 5Last Post: 10-14-2010, 10:41 AM -
2D array of ArrayList (technically a 3D array)
By Lil_Aziz1 in forum New To JavaReplies: 3Last Post: 05-15-2010, 08:11 PM -
Array and arraylist
By MIA6 in forum New To JavaReplies: 3Last Post: 11-29-2009, 02:22 AM -
ArrayList versus Array
By junpogi in forum New To JavaReplies: 13Last Post: 11-08-2008, 02:04 AM -
Array to ArrayList
By javaplus in forum New To JavaReplies: 2Last Post: 11-12-2007, 12:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks