java code Can someone please explain what this is doing step by step! thanks!! p
ID: 673344 • Letter: J
Question
java code Can someone please explain what this is doing step by step! thanks!!
private void shuffleNew() {
ArrayList<Integer> list = new ArrayList<Integer>();
Random random = new Random();
Object[] value = new Object[8];
// get the 9 random number( range from 0 to 9)
// and make sure there is no repetition in it.
while (true) {
int number = random.nextInt(9);
if (!list.contains(number)) {
list.add(number);
}
if (list.size() == 9) {
break;
}
}
value = list.toArray();
for (int i = 0; i < 9; i++) {
System.out.println(value[i]);
}
int count = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
puzzleArray[j][i] = (Integer) value[count];
count++;
}
}
}
Explanation / Answer
// This program generates 10 random numbers between 0 to 9, without duplicating the numbers and prints the values in puzzleArray.
private void shuffleNew() {
// List of Integers Array is defined
ArrayList<Integer> list = new ArrayList<Integer>();
// random object is created here
Random random = new Random();
// value array is created here
Object[] value = new Object[8];
// get the 9 random number( range from 0 to 9)
// and make sure there is no repetition in it.
while (true) {
int number = random.nextInt(9);
if (!list.contains(number)) {
list.add(number);
}
if (list.size() == 9) {
break;
}
}
// 10 random values printed here
value = list.toArray();
for (int i = 0; i < 9; i++) {
System.out.println(value[i]);
}
int count = 0;
// random numbers 2dimension array
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
puzzleArray[j][i] = (Integer) value[count];
count++;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.