Results 1 to 7 of 7
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post Copying ArrayList into an Array

    I have an ArrayList that contains integers and i want to copy it to an array.
    Since arraylist stores Objects only, what can i do to copy my integers to an array???
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  2. #2
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    try using a for loop...and assingments . I used it on an array ones, never on an arraylist.

  3. #3
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    something like ...
    for (int i = 0; i < arrL.length(); ++i)
    {
    array = arrLObj[i].val;
    }

  4. #4
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    something like ...
    for (int i = 0; i < arrL.length(); ++i)
    {
    array = arrLObj[i].val;
    }

  5. #5
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    array = arrLObj[i].val;
    i just don´t understand what is arrLObj[i].val according to your post:confused:
    do you mind expounding alil bit more?

    cheerz!!
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  6. #6
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    Just convert it to an Integer[] and not an int[] like so...

    Java Code:
                    ArrayList<Integer> a = new ArrayList<Integer>();
    
                    ...some other code to add data to arraylist here...
    
    		Integer[] j = (Integer[])a.toArray(new Integer[a.size()]);
    
                    int total = 0;
    		for(int i = 0; i < j.length; i++)
    		{
    			total += j[i].intValue(); //is handled the same
    			total += j[i];                 //as this line 
                    }

  7. #7
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    Thanks alot, got it working:)
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

Similar Threads

  1. Converting ArrayList to Array
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 02:34 PM
  2. [SOLVED] Should I use an arraylist or an array?
    By matzahboy in forum New To Java
    Replies: 5
    Last Post: 11-17-2008, 04:34 AM
  3. ArrayList versus Array
    By junpogi in forum New To Java
    Replies: 13
    Last Post: 11-08-2008, 02:04 AM
  4. deep copying arraylist to add to a list
    By alvations in forum New To Java
    Replies: 13
    Last Post: 10-08-2008, 03:13 PM
  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
  •