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

2
  • Please explain what you want. I suspect people are confused. What do you want result to be after you're done? Commented Apr 4, 2015 at 5:43
  • Check this link for ArrayList of ArrayList stackoverflow.com/questions/25147799/… Commented Apr 4, 2015 at 6:37

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

this simply copy element from one Arraylist to another. what i want is to copy element of num to result in index 0.
@koshishkharel Did the next intitively logig step to try work? That would be result.addAll(0, num).
@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.
@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?
@koshishkharel - What I suggest you do is ... >>think about it<< ... and work it out for yourself. You are a Java programmer, aren't yoo?
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

Comments

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

Comments

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

1 Comment

note that if you're using a version below java 7, you'll have to declare result like this: List<List<Integer>> result = new ArrayList<List<Integer>>();
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.

Comments

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

Comments

-1

Try this sample code

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

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.