17

Is it possible to add the elements of one Arraylist to another Arraylist? For example if an Arraylist has elements 3,6,3,8,5 in index 0,1,2,3,4, now I want to add 3,6,3,8,5 to another ArrayList in index 0, is it possible?

ArrayList<String> num = new ArrayList<String>();
 num.add("3");
 num.add("6");
 num.add("3");
 num.add("8");
 num.add("5");
ArrayList<String> result = new ArrayList<String>();
 for (int i = 0; i < num.size(); i++)
 {
        result.addAll(i,num);   
 }

I have tried this but it is not working. what i want is when i try System.out.println(result.get(0)); result must be [3 6 3 8 5].

Ricky Dev
  • 45
  • 1
  • 6
koshish kharel
  • 347
  • 1
  • 4
  • 12
  • Please explain what you want. I suspect people are confused. What do you want `result` to be after you're done? – ajb Apr 04 '15 at 05:43
  • Check this link for ArrayList of ArrayList http://stackoverflow.com/questions/25147799/java-arraylist-of-arraylist – N Kaushik Apr 04 '15 at 06:37

7 Answers7

35

I think what you are trying to do is this:

for (int i = 0; i < num.size(); i++) {
    result.add(i, num.get(i)); 
}

Or maybe just this:

result.addAll(num);

What your current code does is you add all of num to result many times ... at successive starting positions. That is ... strange.


UPDATE

What i want is when i try System.out.println(result.get(0)); result must be [3 6 3 8 5].

Ah ... I get it ... you are trying to create a list of strings where the strings are representations of the input lists:

Do this:

for (int i = 0; i < num.size(); i++) {
    result.add(i, num.toString()); 
}

This will give you the output you are asking for.

Another possibility is that you want a list of lists of strings:

ArrayList<ArrayList<String>> result = new ArrayList<>();
for (int i = 0; i < num.size(); i++) {
    result.add(i, num); 
}

That will also give you the output you are asking for ... though for a different reason.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • this simply copy element from one Arraylist to another. what i want is to copy element of num to result in index 0. – koshish kharel Apr 04 '15 at 05:52
  • @koshishkharel Did the next intitively logig step to try work? That would be `result.addAll(0, num)`. – glglgl Apr 04 '15 at 05:59
  • @glglgl what if num takes input from file and file contains multiple line. here first line of file is 3,6,3,8,5. each line should be added in different index of result. – koshish kharel Apr 04 '15 at 06:05
  • @stephen C what i trying to do is read the file containing multiple line like 3,6,3,8,5 and add it to Arraylist, my code is adding 3,6,3,8,5 in index 0,1,2,3,4 but i want to add first line in index 0 and second line in index 1 and so on. your suggestion is adding every line to index 0. what can i do? – koshish kharel Apr 04 '15 at 09:40
  • @koshishkharel - What I suggest you do is ... >>think about it<< ... and work it out for yourself. You are a Java programmer, aren't yoo? – Stephen C Apr 04 '15 at 10:32
4

To simply copy all elements you can do

ArrayList<String> result = new ArrayList<String>(num);

Demo

and if you want to copy all the elements at a particular index you have to change the result ArrayList

ArrayList<List<String>> result = new ArrayList<List<String>>();
result.add(0, num);   // 0 is the index

Demo

singhakash
  • 7,891
  • 6
  • 31
  • 65
4
ArrayList<String> num = new ArrayList<String>();
 num.add("3");
 num.add("6");
 num.add("3");
 num.add("8");
 num.add("5");
ArrayList<String> result = new ArrayList<String>();
result.addAll(num);   
1

Your result list needs to be nested. It should have this kind of form:

List<List<Integer>> result = new ArrayList<>();

You can then just do that

result.add(0, num);
jeremie
  • 971
  • 9
  • 19
  • note that if you're using a version below java 7, you'll have to declare result like this: List> result = new ArrayList>(); – jeremie Apr 04 '15 at 05:58
1

do not use all method if you are putting it inside a loop, you should use add

if at all you want to use addAll put it outside the loop.

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
1
ArrayList<String> obj1 = new ArrayList<String>();

obj1.add("3");
obj1.add("6");
obj1.add("3");
obj1.add("8");
obj1.add("5");

ArrayList obj2 = new ArrayList();

obj2.add(0, obj1);;

System.out.println("Size of the arraylist is --> "+obj2.size());

System.out.println("Value at index 0 --> "+obj2);
Anusha B
  • 11
  • 1
-1

Try this sample code

ArrayList<ArrayList<String>> nodes = new ArrayList<ArrayList<String>>();
  ArrayList<String> nodeList = new ArrayList<String>();
   nodes.add(nodeList);
malli
  • 85
  • 2
  • 7
  • http://www.tutorialhall.com/2015/03/how-to-add-arraylist-into-another-arraylist-java-code.html – malli Apr 04 '15 at 05:40