1

So I'm a bit confused about how to use a 2D Array in Java if you only declare the number of rows at first. For example:

int[][] x = new int[5][];

But how would you go on about filling those 5 rows? I'm thinking you need to declare the size of each one of the rows first, but I can't figure out a way to do so. If I try to do something like:

x[0][0] = 5;

The compiler tells me that x[0] is null.

Would appreciate some help, thanks in advance.

Community
  • 1
  • 1
JakeDrone
  • 173
  • 1
  • 9

5 Answers5

2

you tried to define a 2d array: firstly you specified the numbers of row, but you didn't specified the number of columns and its necessary.

for example I defined the 2 columns for the first row and assigned the value:

int[][] x = new int[5][];
x[0] = new int[2];
x[0][0] = 5;
System.out.println(x[0][0]);

for better undestanding of 2d-arrays you need to read more:

https://www.programiz.com/java-programming/multidimensional-array

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
1

Simply do it:

public class ClassName {
    public static void main(String[] args) {
        //Assignment of multi-dimensional array
        int[][] multiarray = new int[2][3];
        // assign value to row "0" and col "0" index
        multiarray[0][0] = 23;
        System.out.println("My Age is:" + multiarray[0][0]);
    }
}
Community
  • 1
  • 1
M Usman
  • 61
  • 3
1

An element of a 2d array is a 1d array. If you define an array directly new int[5][4] then length of each row is 4, otherwise it may vary. When you create a 2d array with undefined row length, each element of it, actually 1d array, is not initialized yet and is null. When you create 1d array of int[] it is initialized with zeros by default. If you define 2d array directly the elements of its rows are initialized with zeros.

Initialization of 2d array

You can define column length without row length:

int[][] arr = new int[5][];

arr[0] = new int[]{1, 2, 3};
arr[3] = new int[2];
arr[3][1] = 9;
arr[4] = new int[]{3, 3, 2, 1, 6, 7};
// output:
[1, 2, 3]
null
null
[0, 9]
[3, 3, 2, 1, 6, 7]

You can define column and row length:

int[][] arr2 = new int[3][3];
arr2[0][0] = 1;
// output:
[1, 0, 0]
[0, 0, 0]
[0, 0, 0]

You can define each element of the 2d array at creation time, optionally with reserved rows for further operations:

int[][] arr3 = new int[][]{{1, 2, 3}, {4, 5}, {6}, null};
// output:
[1, 2, 3]
[4, 5]
[6]
null
0

You can't access x[0][0] because no columns exists at row x[0]. You will need to initialize every column in a row before using it:

x[0] = new int[5];
x[1] = new int[5];

or use a loop:

for (int i = 0; i < 5; i++) {
    x[i] = new int[5];
}
Community
  • 1
  • 1
0
int ROWS = 5;
int COLUMNS = 12;
int[][] x = new int[ROWS][];

Initialize the values of all ROWS to zero:

IntStream.range(0, ROWS).forEach(i -> {
    x[i] = new int[COLUMNS];
});

If You want to initialize to a different value eg. -1:

IntStream.range(0, ROWS).forEach(i -> {
    x[i] = new int[COLUMNS];
    Arrays.fill(x[i], -1);
});
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
Kaplan
  • 2,572
  • 13
  • 14