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 292 views
292 views asked Nov 26, 2023 by avibootz
1 answer 166 views
1 answer 181 views
181 views asked Mar 12, 2023 by avibootz
1 answer 257 views
257 views asked Mar 12, 2023 by avibootz
1 answer 244 views
1 answer 290 views
290 views asked Jun 28, 2020 by avibootz
2 answers 319 views
...