Answer to Question #49529 in Java | JSP | JSF for Gina

Question #49529
How to populate an empty two-dimensional array with random numbers from 0-5?
1
Expert's answer
2014-12-02T00:56:13-0500
//as we receive reference to array we don't need to returnmodified array
//real array is modified at same time
private static void fillArray(int[][] arr) {
if (arr == null) { //if someone sends anull reference
System.out.println("No input array!");
return;
}
java.util.Random random = newjava.util.Random(); //or you can import java.util.Random;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j <arr[i].length; j++) {
arr[i][j] = random.nextInt(5); //5 if from 0 to 5 exlusively and
random.nextInt(6) if from 0 to 5 inclusively
}
}
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS