I have a method that creates an array with a length decided by a parameter in Main. The numbers are created by math random. How do I avoid duplicated by checking the created random number before putting it inside the array each time?
Is it possible to check an unfinished array?
class Test{
public int[] numberArray;
public int length;
public Test(int lengthArray){
this.numberArray=new int[lengthArray];
this.length=lengthArray;
}
public boolean checkArray(int checknumber){
inArray=false;
//what code can I write here to find duplicate
return inArray;
}
public void fillArray(){
int highest = 1000;
int lowest = 100;
int randomNumber = 0;
int counter=0;
for(int i=0;i<length;i++){
randomNumber=lowest + (int)(Math.random() * ((highest - lowest) + 1));
numberArray[counter]=randomNumber;
counter++;
}
}
public class testing {
public static void main(String[] args) {
Test test1 = new Test(9);
test1.fillArray();
for(int value: test1.numberArray){
System.out.print(value+" ");
}
}
}