Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: copy values from arraylist to another

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default copy values from arraylist to another

    Please I need help :/
    How can I copy values from a ArrayList to another.
    if I have a ArrayList containing 10 values and I want to copy the 2nd 4rd values and so on.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: copy values from arraylist to another

    Review the ArrayList API for methods that should be helpful, then post what you've tried when you need help.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default copy values from arraylist to another java

    How can I copy values from a ArrayList to another. if I have a ArrayList containing 10 values and I want to copy the 2nd 4rd values and so on.

    Here's my code:

    ArrayList tab = new ArrayList ();

    ArrayList tab2 = new ArrayList ();

    {

    int i=1;

    int j=0;

    while ( i < tab.size () )

    {

    tab.get(i);

    i=i+2;

    tab2.add(i);

    j=j+1;

    }

    }

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: copy values from arraylist to another java

    Ok, first of all, your loop doesn't make sense.
    while ( i < tab.size () )
    Tab doesn't contain any object so it has a size of 0, but you say while (1 < 0) do something? The code between the brackets will never be reached.



    Just so you know:
    i = i + 2
    is the same as
    i += 2



    The first element of an array has an index of 0
    This means the second element has an index of 1, the forth element has an index of 3, etc
    So you'll probably want to give your i a start value of 0 instead of 1



    What do you use that j variable for? You never use it?


    How I would do it:
    ArrayList<Integer> tab = new ArrayList<>();
        for (int i = 0; i < 10; i++)
            tab.add(i);
     
    ArrayList<Integer> tab2 = new ArrayList<>();
        for (int i = 0; i < 10; i += 2)
            tab2.add(tab.get(i + 1));
    The upper block puts 10 integer values in the tab arraylist, ranging from 0 to 9
    The other block then adds the values of tab at index 1, 3, 5, 7 and 9 to tab2, these are respectively the second, forth, sixth, eight and tenth integer in the tab arraylist

  5. The Following User Says Thank You to UndersKore For This Useful Post:

    e93 (January 2nd, 2014)

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: copy values from arraylist to another java

    Duplicate topic. This one has more effort.

    Please read this topic to learn how to post code correctly and other useful topics for newcomers.

  7. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: copy values from arraylist to another java

    in the second arraylist shouldn't it be get(i) not i+1 ?
    and for int i =1?

  8. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: copy values from arraylist to another

    for (int i = 0; i < 10; i += 2)
    The variable i will respectively go trough the following values:
    0, 2, 4, 6, 8

    tab2.add(tab.get(i + 1));
    i + 1 then gives these values:
    1, 3, 5, 7, 9

    And this are the indexes that are required for what you want to do (:

    I'm also still new to Java and my teacher told me that you should try to always give your integer variable in such a for-next loop a value of zero. Good programming practice I guess!

  9. The Following User Says Thank You to UndersKore For This Useful Post:

    e93 (January 3rd, 2014)

Similar Threads

  1. How to copy one array into another and flip the values
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 9th, 2013, 03:27 PM
  2. [SOLVED] Return all values in an ArrayList from a method
    By IanSawyer in forum Collections and Generics
    Replies: 1
    Last Post: March 28th, 2012, 09:53 AM
  3. Replies: 3
    Last Post: May 21st, 2011, 08:47 AM
  4. Deep copy of ArrayList?
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2011, 01:30 PM
  5. Help with Finding Max and Min Values of ArrayList
    By CheekySpoon in forum Collections and Generics
    Replies: 3
    Last Post: March 2nd, 2011, 07:57 PM