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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to initialize and print sublists of multiple ArrayLists (List of Lists) in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.List;
  
public class MyClass {
    public static void main(String args[]) {
 
        List<List<String>> listOfLists = new ArrayList<>();
 
        List<String> subList1 = new ArrayList<>();
        subList1.add("c#");
        subList1.add("c++");
 
        List<String> subList2 = new ArrayList<>();
        subList2.add("c");
        subList2.add("java");
        subList2.add("php");
 
        listOfLists.add(subList1);
        listOfLists.add(subList2);
  
        System.out.println(listOfLists);
        System.out.println();
        
        for(List<String> sub : listOfLists) {
            System.out.println(sub);
        }  
        System.out.println();

          
        for(List<String> sub : listOfLists) {
            System.out.println(sub.get(0) + "  " + sub.get(1));
        } 
        System.out.println();

        listOfLists.forEach(innerList -> {
            String sub = String.join(", ", innerList);
            System.out.println(sub);
        });
    }
}
    
    
    
    
/*
run:
    
[[c#, c++], [c, java, php]]

[c#, c++]
[c, java, php]

c#  c++
c  java

c#, c++
c, java, php
    
*/

 



answered Oct 14, 2023 by avibootz
edited Oct 14, 2023 by avibootz
...