+ 2
Java beginner's question
I'm working through the book Java Beginner's Guide 6th Edition and am learning about multidimensional arrays. I copied out the example on page 157 and understand most of it but can't get my head around the code in line 11 where the arrays are filled with some data. Please someone explain it to me exactly what this is doing. It's the last line of this part: //give nums some values for (int i = 0; i < 3; i++) for (int j = 0; j < 5; j++) nums[i][j] = (i+1)*(j+1); <------ THIS BIT https://code.sololearn.com/cBJCEfL93G1H/#java
2 Answers
+ 4
for (int i = 0; i < 3; i++)
for (int j = 0; j < 5; j++)
nums[i][j] = (i+1)*(j+1);
its simple, comment says, it is assigning some values to 3x5 array. In this way for example:
nums[2][3]=(2+1)*(3+1)=3*4=12
+ 2
@Daniel, simple when you know how LOL. Thank you for explaining. I realised when you said it's a 3x5 array that I was forgetting that a 2d array is like a table and that the result of line 11 goes into a 'box' specified by a two number reference i.e. i & j. It seemed to me it was putting one number into each of two boxes and it didn't make any sense. Many thanks