I have a class that describes a set of numbers. One of the questions is "create a method that receives a random amount of numbers and makes it into a set of numbers" At first I thought it was array and I wrote the code below, but when I looked at what was requested in the main program is Set s1 = new Set (2,6,1,2,2,6,7,19,5); While Set s1 = new Set (random amount of numbers); I need to create a constructor for it, is there a way to find out how many numbers should be there without using arrays?
public Set(int[] se)
{
if(se.length < 2)
{
System.out.println("Array smaller than 2, please change the size!");
return;
}
int[] temp = se;
int newLength = temp.length;
for (int i = 1; i < temp.length; i++)
{
for (int j = 0; j < i; j++)
{
if (temp[i] == temp[j])
{
newLength--;
break;
}
}
}
s = new int[newLength];
s[0] = temp[0];
int inx = 1;
boolean isDuplicate;
for (int i = 1; i < temp.length; i++)
{
isDuplicate = false;
for (int j = 0; j < i; j++)
{
if (temp[i] == temp[j])
{
isDuplicate = true;
break;
}
}
if (!isDuplicate)
{
s[inx] = temp[i];
inx++;
}
}
}