How to print dynamic 2D ArrayList in java

1 Answer

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

public class MyClass {
    public static void main(String args[]) {
        List<int[]> li = new ArrayList<>();
        
        li.add(new int[]{6, 8, 1, 2});
        li.add(new int[]{9, 3, 4});
        li.add(new int[]{5, 7});
        
        for (int i = 0; i < li.size(); i++) {
            int[] arr = new int[4]; 
            arr = li.get(i);
            for (int j = 0;j < arr.length; j++) {
                System.out.printf("%2d", arr[j]); 
            }
            System.out.println();
        }   
    }
}
 
 
 
/*
run:
 
 6 8 1 2
 9 3 4
 5 7
 
*/

 



answered Mar 22, 2021 by avibootz

Related questions

1 answer 187 views
3 answers 261 views
261 views asked Mar 22, 2021 by avibootz
2 answers 231 views
1 answer 124 views
1 answer 214 views
1 answer 183 views
...