java - Declare Two-Dimensional Array - Clarification -
i taking java lessons, , course explaining two-dimensional arrays. first method declaring type of variable create constant arguments how many rows , columns in two-dimensional array. then, used nested for-loop give random numbers particular rows, , columns.
the second method declare , initialize two-dimensional array "int[][] nums" line below. know has 3 rows. it's similar putting list in bigger list, how many columns in "nums" array? there may simple answer, little confused right now. thank assistance.
import java.util.random; //importing class package public class chap15part4 { public static void main(string[] args) { final int rows = 5; //constant final int cols = 5; //constant int[][] numbers = new int[rows][cols]; //declaring 2-d array constants arguments random rand = new random(system.currenttimemillis()); //seeding random number generator for(int r = 0; r < rows; ++r) //nested for-loop assign numbers elements (int c = 0; c < cols; ++c) numbers[r][c] = rand.nextint(101); int[][] nums = {{10,20,30,40}, {20,30,40,50}, {30,40,50,60}}; //declaring & initializing 2-d array } }
there 4 columns. matrix this, can count columns easily:
{{10, 20, 30, 40}, {20, 30, 40, 50}, {30, 40, 50, 60}}
Comments
Post a Comment