Results 1 to 15 of 15
  1. #1
    nanees is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Thumbs up how to concatenate two arraylists in a third one

    hey all ..
    its my first time to work on netbeans and learning java .
    i have two arraylists in my program and i want to add the first word in each arraylist in a third one sequentially

    for example:
    first arraylist second arraylist
    one 1
    two 2
    three 3

    i want the third one to be
    one 1
    two 2
    three 3
    Last edited by nanees; 03-31-2011 at 04:37 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default

    Word to the wise- saying things like "i want a answer and waiting on ur solutions" will make people ignore your post, for several reasons. It makes you seem lazy, rude, and impatient.

    With that in mind, check out the API for List.
    Last edited by KevinWorkman; 03-31-2011 at 03:29 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    nanees is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    thank you and sorry its my first time today to join this website :$

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    //declare list with Type (String)
    List<String> myArrayList = new ArrayList<String>();

    //get item by index
    String firstItem = myArrayList.get(0);
    String secondItem = myArrayList.get(1);
    String NthItem = myArrayList.get(n-1);

    //add items to list
    String thirdItem = "Three";
    List<String> mySecondList = new ArrayList<String>();
    mySecondList.add(thirdItem);

  5. #5
    nanees is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    thank you .. but im afraid i tried this method i get errors because im using two arraylists not one like in your example ... is there any concatenate method i could add items by it ?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,514
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nanees View Post
    thank you .. but im afraid i tried this method i get errors because im using two arraylists not one like in your example ... is there any concatenate method i could add items by it ?
    If one list is { a, b, c } and the second list is { d, e, f }, do you want the result to be { ad, be, cf }?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    arrayList1.addAll(arrayList2)

    always look at the API methods if you're not sure
    ArrayList (Java 2 Platform SE v1.4.2))

  8. #8
    nanees is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    yes thats exactly what i need

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I think he meant to say that to JosAh, he needs it to be { ad, be, cf }

    but there is no built-in method for that so he'd need a loop

  10. #10
    nanees is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    yeah i want what Josah says ... to concatenate the two strings in a new arraylist ..
    but im just new in java .. only working with it a couple of months so i don't know how to make it :$

    btw ozzyman im a girl =]]

  11. #11
    BOJANM is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

  12. #12
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    my bad, she* :P

    i can help you get started but you can't expect people to write all your code for you

    what haven't you learnt that you can't do?

    //add two strings together
    String firstString = "a";
    String secondString = "d";
    String ad = firstString + secondString;

    //loop through array list
    for (int i=0; i<myArrayList.size(); i++) {

    }

    //retrieve element from list by index
    myList.get(index);

    you logic should be like this:

    Java Code:
    list 1 {a, b, c}
    list 2 {d, e, f}
    
    loop through list 1 {
        list 1.get(index) += list 2.get(index)
    }

  13. #13
    nanees is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    really thank you ..
    your loop thats what i need
    thanks again for your help =]]

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,514
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nanees View Post
    yes thats exactly what i need
    So both Lists have to be the same size; here's a spoiler:

    Java Code:
    List<String> result= new ArrayList<String>();
    for (int i= 0; i < a.size(); i++) // both a and b are your lists
       result.add(a.get(i)+b.get(i));
    You fit that code snippet somewhere in your code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    nanees is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

Similar Threads

  1. How to concatenate a number
    By jim01 in forum New To Java
    Replies: 4
    Last Post: 03-27-2011, 02:31 AM
  2. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 04:27 AM
  3. Concatenate year from DATETIME
    By ashin in forum SWT / JFace
    Replies: 11
    Last Post: 07-30-2010, 05:34 PM
  4. Concatenate Linked Lists
    By tttestall in forum New To Java
    Replies: 1
    Last Post: 04-20-2010, 07:03 PM

Tags for this Thread

Posting Permissions

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