Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,442 answers

573 users

How to create ArrayList of ArrayLists in Java

1 Answer

0 votes
import java.util.*;
   
public class MyClass {
    public static void main(String args[]) {
        ArrayList<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>(4); 
  
        ArrayList<Integer> al1 = new ArrayList<Integer>(); 
        al1.add(1); 
        al1.add(2); 
        al1.add(3); 
        al.add(al1); 
  
        ArrayList<Integer> al2 = new ArrayList<Integer>(); 
        al2.add(4); 
        al2.add(5); 
        al.add(al2); 
  
        ArrayList<Integer> al3 = new ArrayList<Integer>(); 
        al3.add(6); 
        al3.add(7); 
        al3.add(8); 
        al3.add(9); 
        al.add(al3); 

        ArrayList<Integer> al4 = new ArrayList<Integer>(); 
        al4.add(78); 
        al4.add(99); 
        al4.add(100); 
        al4.add(89); 
        al4.add(90); 
        al.add(al4); 
  
        for (int i = 0; i < al.size(); i++) { 
            for (int j = 0; j < al.get(i).size(); j++) { 
                System.out.print(al.get(i).get(j) + " "); 
            } 
            System.out.println(); 
        } 
    }
}
   
   
   
   
/*
run:
   
1 2 3 
4 5 
6 7 8 9 
78 99 100 89 90 
   
*/

 



answered Apr 22, 2020 by avibootz
edited Apr 22, 2020 by avibootz

Related questions

2 answers 198 views
1 answer 165 views
1 answer 209 views
1 answer 215 views
215 views asked Apr 22, 2020 by avibootz
1 answer 224 views
1 answer 263 views
263 views asked Jan 16, 2022 by avibootz
...