1

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++;
        }
    }
}
  • 3
    you can utilize java [varargs](https://www.geeksforgeeks.org/variable-arguments-varargs-in-java/) mechanism – Sharon Ben Asher Jan 30 '19 at 08:43
  • @SharonBenAsher That is not his question. It is about what happens **inside** the constructor. How that array gets into it doesn't really matter. – GhostCat Jan 30 '19 at 08:44
  • 1
    ok, perhaps OP can clarify what is the problem? what is the meaning of " find out how many numbers should be there " – Sharon Ben Asher Jan 30 '19 at 08:47
  • @SharonBenAsher See my answer. He wants to know if how to avoid having that temp array. The point is: a set has to eliminate all duplicates, and then store all unique values in array with the exact size required. So you need to identify/count the duplicates. – GhostCat Jan 30 '19 at 08:50
  • @GhostCat, we differ in our interpratation of the question. until OP bothers to clarify, I remain uncinvinced that this was the intention. OP qoutes the calling method without mentioning duplicates – Sharon Ben Asher Jan 30 '19 at 08:52
  • You can use varargs jdk 1.5 feature. Visit : https://www.geeksforgeeks.org/variable-arguments-varargs-in-java/ – Bishal Dubey Jan 30 '19 at 09:05
  • I will clarify right now, I've been working on this problem for a couple of hours now and still can't figure it out. @GhostCat is right. I am looking for a way to eliminate all duplicates, and store unique values in an array. Though the numbers I receive are actual numbers and not an array and the amount I am receiving may vary. One time it may be Set s1 = new Set (2,6,1,2,2,6,7,19,5) and the other time it may be Set s1 = new Set (5,42,3) How do I know how many numbers are in the brackets. – Alen Komras Jan 30 '19 at 09:28
  • Your "clarification" doesnt help. There is only **one** reasonable way to have new Set(1, 2, 3) to be legal, and Set(), and Set(1,2,5,6,7) - and that is in fact by using the vararg ... notation. And then your constructor gets passed an array. And you just ask that array for its length, and you know how many numbers were passed **in**. But that is exactly not how I understood your question ;-) ?! – GhostCat Jan 30 '19 at 09:32
  • @BishalDubey, I will look into it right now – Alen Komras Jan 30 '19 at 09:33

0 Answers0