I am in a cs 2010 class. Haven't ever worked on coding before or anything of the sort. I have an okay teacher but he has a very thick accent that is hard to understand. He recently gave us a project to complete over a few days. I have been having problems getting the last part of the project done.
The project asks you to generate 10,000 random numbers between 0-9999 and arrange them in an array of 10,000 numbers without repeating any of them. As you can see, this is basically asking you to make the array put the numbers 0-9999 in an array in order of least to greatest. My problem is the non-repeating numbers. I have been working on the code for over 4 hours trying to figure out how to make it not repeat and have had no luck. I have searched online for at least an hour and all other hints or solutions have not helped. This is the code I have so far, can anyone please help me?
package array.sorter.project;
import java.util.Arrays;
import java.util.Random;
public class Sorting {
public static void main(String args[]){
int[] randomNumbers = new int[10000];
Random rand = new Random();{
for (int i = 1; i < randomNumbers.length; i++) {
int n = rand.nextInt(10000);
randomNumbers[i] = n;}
for (int i = 0; i < randomNumbers.length; i++) {
int smallestNo = randomNumbers[i];
int posWithSmallest = i;
for (int j = i+1; j < randomNumbers.length; j++) {
int val = randomNumbers[j];
if (val < smallestNo) {
smallestNo = val;
posWithSmallest = j;
}
}
int tmp = randomNumbers[i];
randomNumbers[i] = smallestNo;
randomNumbers[posWithSmallest] = tmp;
}
Arrays.sort(randomNumbers);
for (int i = 0; i < randomNumbers.length; i++) {
System.out.println("Position " + i + " : " + randomNumbers[i]);
}
}
}
}