Pretty new to this and I've exhausted numerous attempts to figure out why this isn't working but can't get it.
private int indexOf(int searchingNum)
{
int x = searchingNum;
for (int i = 0; i<numberArray.length; i++){
if (numberArray[i]==x)
index = i;
else
index = -1;
}
return index;
}
public boolean addNumber(int numberToAdd)
{
int x = numberToAdd;
if (indexOf(x)!=-1)
return false;
if (count<numberArray.length&&indexOf(x)==-1){
count++;
numberArray[count-1] = x;
return true;
}
if (count>=numberArray.length&&indexOf(x)==-1){
count++;
newArray = Arrays.copyOf(numberArray, 2* numberArray.length);
numberArray = newArray;
numberArray[count-1] = x;
}
return true;
}
The method should't allow for duplicates but does. Any pointers in the right direction would be much appreciated.
Thank you.
Setinstead of an array.