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,855 questions

51,776 answers

573 users

How to create dynamic 2D ArrayList in java

3 Answers

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});
        li.add(new int[]{9, 3});
        li.add(new int[]{2});
        
        System.out.println("Element [0][0]: " + li.get(0)[0]);
        System.out.println("Element [0][1]: " + li.get(0)[1]);
        System.out.println("Element [0][1]: " + li.get(0)[2]);
        
        System.out.println("Element [0][1]: " + li.get(1)[0]);
        System.out.println("Element [0][1]: " + li.get(1)[1]);
        
        System.out.println("Element [0][1]: " + li.get(2)[0]);
    }
}
 
 
 
/*
run:
 
Element [0][0]: 6
Element [0][1]: 8
Element [0][1]: 1
Element [0][1]: 9
Element [0][1]: 3
Element [0][1]: 2
 
*/

 



answered Mar 22, 2021 by avibootz
0 votes
import java.util.ArrayList;
import java.util.Arrays;
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});
        li.add(new int[]{9, 3});
        li.add(new int[]{2});
        
        for (int[] row : li) {
            System.out.println("Row = " + Arrays.toString(row));
        } 
    }
}
 
 
 
/*
run:
 
Row = [6, 8, 1]
Row = [9, 3]
Row = [2]
 
*/

 



answered Mar 22, 2021 by avibootz
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});
        li.add(new int[]{9, 3});
        li.add(new int[]{2});
        
        for (int i = 0; i < li.size(); i++) {
            int[] arr = new int[3]; 
            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
 9 3
 2
 
*/

 



answered Mar 22, 2021 by avibootz
edited Mar 22, 2021 by avibootz

Related questions

1 answer 154 views
1 answer 163 views
163 views asked Mar 22, 2021 by avibootz
2 answers 195 views
1 answer 141 views
1 answer 173 views
173 views asked Jan 16, 2022 by avibootz
1 answer 127 views
2 answers 161 views
...