Results 1 to 15 of 15
- 03-31-2011, 03:19 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
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 3Last edited by nanees; 03-31-2011 at 04:37 PM.
- 03-31-2011, 03:24 PM #2
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!
- 03-31-2011, 03:26 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
thank you and sorry its my first time today to join this website :$
-
//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);
- 03-31-2011, 03:55 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
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 ?
- 03-31-2011, 04:01 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,411
- Blog Entries
- 7
- Rep Power
- 17
-
arrayList1.addAll(arrayList2)
always look at the API methods if you're not sure
ArrayList (Java 2 Platform SE v1.4.2))
- 03-31-2011, 04:02 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
yes thats exactly what i need
-
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
- 03-31-2011, 04:08 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
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 =]]
- 03-31-2011, 04:09 PM #11
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
-
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) }
- 03-31-2011, 04:30 PM #13
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
really thank you ..
your loop thats what i need
thanks again for your help =]]
- 03-31-2011, 05:03 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,411
- Blog Entries
- 7
- Rep Power
- 17
So both Lists have to be the same size; here's a spoiler:
You fit that code snippet somewhere in your code.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));
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-31-2011, 05:08 PM #15
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
How to concatenate a number
By jim01 in forum New To JavaReplies: 4Last Post: 03-27-2011, 02:31 AM -
ArrayLists
By Freakzoyd in forum New To JavaReplies: 4Last Post: 11-12-2010, 04:27 AM -
Concatenate year from DATETIME
By ashin in forum SWT / JFaceReplies: 11Last Post: 07-30-2010, 05:34 PM -
Concatenate Linked Lists
By tttestall in forum New To JavaReplies: 1Last Post: 04-20-2010, 07:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks