+ 1
If you want make it 4x4 then it is simple :
Declare new array :
int arN[][] = new int[4][4] ;
First you can copy 3x3 and at last position, you can assign like
arN[i][3] = 0; #any number , where i = 0,1,2,3. You can assign 4 elements..
Array will be :
1 2 3 0
4 5 6 0
7 8 9 0
If you want varying length rows, then try :
int arr[][] = new int[3][]; // columns size yet not initialized.. so you can't assign or use until you initialize memory.
arr[0] = new int[]{2,3,4}; // will declare and initialized first row .
arr[1] = new int[arr[0].length+1]; // this columns have length 4.
You can initialize like
arr[1][0] = 7;
arr[1][1] = 8;
arr[1][2] = 9;
arr[1][3] = 10;
Arrays.toString( arr[1] ) ; // prints this row : [ 7,8,9,10]
arr[2] = new int[5];
Initialized total array is :
2 3 4
7 8 9 10
0 0 0 0 0 // initialized but not assinged any values. So defaults.
Hope it helps....
+ 1
It's a 4x2 2d array. What is your difficulties with your code? Is with last 2 statements?
You are missing assignment value.
Arrays.toString method works with 1d Array only
+ 1
3x3 2d array Declaration :
int arr[][] = new int[3][3] ;
You can add elements by using 2 loops:
for( int row = 0; row < 3; row++)
for( int col = 0; col < 3 ; col++)
arr[ row ] [ col ] = scanner_obj.nextInt() ; // by input
row represents rows
Col represents column of 2d matrix.
You can print matrix, same way by using loops ..
Sample array will be :
1 2 3
4 5 6
7 8 9
arr.length returns length of columns, i.e the number of column the matrix has..
If you want a particular row size, then use arr[ i ].length ;
Do you want add element to every column? Or to a special column?
+ 1
👍🏼👍🏼👍🏼👍🏼
0
I don't understand your expected code.. How you want your code to perform.. For what? Do you want to print 2d array?