Results 1 to 4 of 4
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default 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:

    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];
    }
    However this is an O(n) operation and not very neat. Is there a better way?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by imorio View Post
    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:

    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];
    }
    However this is an O(n) operation and not very neat. Is there a better way?
    There's also a second 'toArray( ... )' method that can do the job for you; note that it is still an O(n) operation but you don't have to do the copying.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by imorio View Post
    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.
    Yup, just imagine what could happen if you change the array contents (shared by the ArrayList). The entire array need not be filled.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Array vs ArrayList
    By Lund01 in forum New To Java
    Replies: 5
    Last Post: 10-14-2010, 10:41 AM
  2. 2D array of ArrayList (technically a 3D array)
    By Lil_Aziz1 in forum New To Java
    Replies: 3
    Last Post: 05-15-2010, 08:11 PM
  3. Array and arraylist
    By MIA6 in forum New To Java
    Replies: 3
    Last Post: 11-29-2009, 02:22 AM
  4. ArrayList versus Array
    By junpogi in forum New To Java
    Replies: 13
    Last Post: 11-08-2008, 02:04 AM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 12:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •