koshish kharel
koshish kharel

Reputation: 347

Add elements of one ArrayList to another ArrayList

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].

Upvotes: 17

Views: 87177

Answers (7)

user9562951
user9562951

Reputation: 41

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);   

Upvotes: 4

Anusha B
Anusha B

Reputation: 11

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);

Upvotes: 1

Stephen C
Stephen C

Reputation: 718718

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.

Upvotes: 35

singhakash
singhakash

Reputation: 7919

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

Upvotes: 4

Saurabh Jhunjhunwala
Saurabh Jhunjhunwala

Reputation: 2922

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.

Upvotes: 1

jeremie
jeremie

Reputation: 1131

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);

Upvotes: 1

malli
malli

Reputation: 85

Try this sample code

ArrayList<ArrayList<String>> nodes = new ArrayList<ArrayList<String>>();
  ArrayList<String> nodeList = new ArrayList<String>();
   nodes.add(nodeList);

Upvotes: -1

Related Questions