How to join two ArrayList in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        ArrayList<Integer> al1 = new ArrayList<>(Arrays.asList(40, 89, 23, 99, 89)); 
        ArrayList<Integer> al2 = new ArrayList<>(Arrays.asList(7, 8, 2, 0)); 
        
        System.out.println(al1); 
        System.out.println(al2); 
        
        ArrayList<Integer> al12 = new ArrayList<Integer>();
        
        al12.addAll(al1);
        al12.addAll(al2);
        
        System.out.println(al12); 
   
    }
}
     
     
   
     
     
/*
run:
     
[40, 89, 23, 99, 89]
[7, 8, 2, 0]
[40, 89, 23, 99, 89, 7, 8, 2, 0]

*/

 



answered Aug 22, 2021 by avibootz
edited Aug 22, 2021 by avibootz

Related questions

2 answers 187 views
187 views asked Nov 26, 2023 by avibootz
1 answer 108 views
1 answer 108 views
108 views asked Mar 12, 2023 by avibootz
1 answer 174 views
174 views asked Mar 12, 2023 by avibootz
1 answer 169 views
1 answer 210 views
210 views asked Jun 28, 2020 by avibootz
2 answers 200 views
...