+ 6
How one can fill a multidimensional array in Java?
Could you please suggest any proper way of filling a multidimensional array with random numbers in Java? I am particularly interested in those using for loops.
6 ответов
+ 7
My example with 2D array filled with random numbers between 0 and 10. Hope it helps you.
https://code.sololearn.com/cx6oKVOQW37z/?ref=app
+ 3
Thank you very much for your answer! It is very helpful!
+ 2
You are welcome 😉
+ 2
JAVA
Multidimensional Arrays
Multidimensional arrays are array that contain other arrays. The two-dimensional array is the most basic multidimensional array.
To create multidimensional arrays, place each array within its own set of square brackets. Example of a two-dimensional array:
int[ ][ ] sample = { {1, 2, 3}, {4, 5, 6} };
This declares an array with two arrays as its elements.
To access an element in the two-dimensional array, provide two indexes, one for the array, and another for the element inside that array.
The following example accesses the first element in the second array of sample.
int x = sample[1][0];
System.out.println(x);
// Outputs 4
The array's two indexes are called row index and column index.
Multidimensional Arrays
You can get and set a multidimensional array's elements using the same pair of square brackets.
Example:
int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} };
myArr[0][2] = 42;
int x = myArr[1][0]; // 4
The above two-dimensional array contains three arrays.
+ 2
The easiest way is to use nested for loops.
+ 2
Multiple Arrays are very useful for statistical and electrical engineering work. I have written APPS for addition, subtraction and multiplication on N x M matrix. I wrote in user friendly Visual Basic using Microsoft Visual Studios 10.