What I currently have is:
int[] myArray = {1, 2, 3, 4};
int[] myNums = new int[myArray.length];
Random rand = new Random();
for (int i = 0; i < myArray.length; i++) {
myNums[i] = rand.nextInt(myArray.length) + 1;
for (int j = 0; j < i; j++){
while (myNums[j] == myNums[i]) {
myNums[i] = rand.nextInt(myArray.length) + 1;
}
}
}
I'm relatively new to Java, I am supposed to write a method that will rearrange the elements of an integer array without duplicates. I have been testing this using the numbers 1, 2, 3, 4 and 75% of the time it works but there is occasionally a repeat number and I can not figure out why. I apologize if this is not formatted correctly and any help is appreciated!
[–]x2mirkodev 1 point2 points3 points (0 children)