How do i initialize a 2D Array with m(rows), n(cols) in java?

sai venkat

I wanted to initialize a 2D arraylist of size 2n-1(rows), n(cols)

    int num = 3; 
    // declare an arrayList of ArrayLists or 2D ArrayList
    ArrayList<ArrayList<Integer>> list =  
           new ArrayList<ArrayList<Integer>>(num);

The above syntax is to create 2D ArrayList of size Num*Num.

In arrays we initialize with M*N, similarly can we do with ArrayLists? if yes, how?

I want syntax to initialize with M*N.

DevilsHnd

Unlike an Array which must be initialized to a fixed length, an ArrayList can grow dynamically, you don't have to worry about M and N to initialize it. You worry about that when you want to fill the ArrayList and if filling the collection to M and N is your specific requirement then do so after the ArrayList has been declared. So, ArrayList<ArrayList<Integer>> list = new ArrayList<>(); is all you need.

There are a number of ways to fill the ArrayLists to your desired M (rows) and N (columns). Here is a relatively decent visual way to do it.

ArrayList<ArrayList<Integer>> list = new ArrayList<>();
ArrayList<Integer> innerList;
int m = 20;  // Rows
int n = 10;  // Columns 
int incrementer = 1;

for (int i = 0; i < m; i++) {
    innerList = new ArrayList<>();
    for (int j = 0; j < n; j++) {
        innerList.add(j + incrementer);
    }
    list.add(innerList);
    incrementer ++;
}
    
// Now to display (print) the 2D ArrayList into the Console Window:
for (ArrayList<Integer> inner : list) {
    for (Integer ints : inner) {
        System.out.printf("%-3s ", ints);
    } 
    System.out.println();
}

The console should display:

1   2   3   4   5   6   7   8   9   10  
2   3   4   5   6   7   8   9   10  11  
3   4   5   6   7   8   9   10  11  12  
4   5   6   7   8   9   10  11  12  13  
5   6   7   8   9   10  11  12  13  14  
6   7   8   9   10  11  12  13  14  15  
7   8   9   10  11  12  13  14  15  16  
8   9   10  11  12  13  14  15  16  17  
9   10  11  12  13  14  15  16  17  18  
10  11  12  13  14  15  16  17  18  19  
11  12  13  14  15  16  17  18  19  20  
12  13  14  15  16  17  18  19  20  21  
13  14  15  16  17  18  19  20  21  22  
14  15  16  17  18  19  20  21  22  23  
15  16  17  18  19  20  21  22  23  24  
16  17  18  19  20  21  22  23  24  25  
17  18  19  20  21  22  23  24  25  26  
18  19  20  21  22  23  24  25  26  27  
19  20  21  22  23  24  25  26  27  28  
20  21  22  23  24  25  26  27  28  29  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I declare and initialize an array in Java?

How do I do a deep copy of a 2d array in Java?

How do I initialize a byte array in Java?

How do I initialize a Graphics object in Java?

How do I initialize an array of vectors?

How do I initialize an array of arrays in BCPL?

How do I initialize an array in a struct

How can I initialize 2d array with a list of 1d arrays?

How do I create a 2d array made of 2d int arrays in java?

How do I initialize an array of buttons in Kotlin

OpenCV How to initialize Mat with 2D array in JAVA

How do I create and initialize an immutable array?

How do I store, sort and print a 2D "array" of strings and doubles in Java?

How do I initialize an array in a default constructor?

How do I get the function to print out a 2D array into two files in java?

How do i convert a Set into 2D array in java?

How to initialize a 2d array and how to access it from a main method in java?

How do I find path using 2d array maze in java

How do I insert a row into a 2D array in java?

How do I replace a specific string in a 2d array?

How do I sort a 2D array?

How do I use user input to select data from a 2D array in Java?

How do I initialize a pointer to an Array of structure

How do I create a table using 2D array in Java?

how do I initialize a 3D array with two 2D array in python?

How do I add an array into an arraylist (so that it becomes a 2D arraylist) in java?

How do I enter data from user input into a 2D array in Java?

How do I fill my 2D array with a string ? Java

How do I use a loop to multiply a row of a 2D array to the column of a second 2D array, and etc in Java?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive