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

51,772 answers

573 users

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 124 views
3 answers 211 views
211 views asked Mar 22, 2021 by avibootz
3 answers 181 views
3 answers 187 views
...