How to create dynamic two dimensional (2D) array in Java

2 Answers

0 votes
package javaapplication1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            List<int[]> list = new ArrayList<int[]>();

            list.add(new int[]{0, 1, 2, 3, 5});
            list.add(new int[]{6, 7, 8, 9});
            list.add(new int[]{12, 13, 17});
            list.add(new int[]{9});

            list.stream().forEach((row) -> {
                System.out.println("Row = " + Arrays.toString(row));
            }); 

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

Row = [0, 1, 2, 3, 5]
Row = [6, 7, 8, 9]
Row = [12, 13, 17]
Row = [9]
    
 */

 



answered Nov 25, 2016 by avibootz
0 votes
package javaapplication1;

import java.util.ArrayList;
import java.util.List;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            List<int[]> list = new ArrayList<int[]>();

            list.add(new int[]{0, 1, 2, 3, 5});
            list.add(new int[]{6, 7, 8, 9});
            list.add(new int[]{12, 13, 17});
            list.add(new int[]{9});

            System.out.println(list.get(0)[0]); // 0
            System.out.println(list.get(1)[1]); // 7
            System.out.println(list.get(2)[1]); // 13

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

0
7
13
    
 */

 



answered Nov 25, 2016 by avibootz
edited Nov 25, 2016 by avibootz

Related questions

2 answers 150 views
3 answers 252 views
252 views asked Mar 22, 2021 by avibootz
3 answers 211 views
3 answers 218 views
...