Is there something wrong with my Intset class?

I am creating a new IntSet class that uses an ArrayList. Firstly, I extend the Intset ArrayList and run the implementation method. I am facing some problems in my union () method. here is my code ...

public class IntSet extends ArrayList<Integer>{ private static final long serialVersionUID = 1L; private ArrayList<Integer> intset; public IntSet(){ this.intset = new ArrayList<Integer>(); } public IntSet(ArrayList<Integer> intset){ this.intset = intset; } public void insert(int x){ this.intset.add(x); } @Override public Integer remove(int x){ int index = intset.indexOf(x); this.intset.remove(index); return 1; } @Override public int size(){ return this.intset.size(); } @Override public Integer get(int index){ return this.intset.get(index); } public boolean member(int x){ if(intset.indexOf(x)==-1) return false; else return true; } public IntSet union(IntSet a){ IntSet intersectSet = new IntSet(); intersectSet.insert(0); intersectSet.insert(1); System.out.println(intersectSet.size()); System.out.println(intersectSet.contains(1)); for(int i=0; i<a.size(); i++){ } return intersectSet; } public String toString(){ if(intset.size()==0) return "[]"; String s = "[" + intset.get(0).toString(); for(int i=1; i<intset.size(); i++){ s += "," + intset.get(i).toString(); } return s += "]"; } } 

In method

 union(IntSet a); 

I create a new Intset object, then add 2 values ​​(0, 1) to the intersectSet variable.

 intersectSet.insert(0); intersectSet.insert(1); 

then I print the intersectSet size, it showed me 2, which is correct!

but when do I need to check that there is 1 in intersectSet or not? he showed me a lie.

 System.out.println(intersectSet.contains(1)); 

In fact, it should show me true, because intersectSet is an integer 1.

Is there something wrong in my code, and should I extend the ArrayList for the IntSet class?

+4
source share
4 answers

Some suggestions for class design:

  • You do not have an extension to the ArrayList class. A β€œset” really should not expand the list. However, you should probably implement Set. This will give an additional bonus to the compiler telling you what methods you need to implement for the set .....
  • For maximum performance (but more work!) You can use an internal array rather than an ArrayList.
  • Consider making the structure immutable, with functions that return a new copy rather than mutating the set in place. Depending on your use, this may be the best solution, especially if you are mostly dealing with small, non-changing sets.
  • Again, depending on your use, you can override hashCode and equal implementation of equality based on value.
  • When you create an Intset with an ArrayList, you should ideally protect (clone) the ArrayList. You do not want you to change if someone mutates the original ArrayList.
+2
source

The problem is that you actually have 2 ArrayLists. Class IntSet IS A ArrayList , but this class contains a second ArrayList IntSet . Get rid of one of these ArrayLists. To demonstrate this, add this second line:

 System.out.println(intersectSet.contains(1)); System.out.println(intersectSet.intset.contains(1)); 

this will output:

 false true 

So, you need to make a choice whether I inherit from ArrayList or contain ArrayList. Of course, what I get here is point 16 of Effective Java, Enjoy composition over inheritance .

+1
source

You extend the ArrayList and manage your own internal ArrayList object, which means that for all the methods you have overridden, you interact with your intset member intset , otherwise you interact with the inherited inner representation used by the ArrayList Superclass. If you override the contains method, you get the correct behavior.

I suggest dropping the subclass of ArrayList and implementing List or Set instead, although this depends on the specific problem you asked to solve.

0
source

you need to override the contains method.

 public boolean contains(Object o) { return intset.contains(o); } 

and other ArrayList methods associated with its elements.

and I don’t think it’s a good solution. you can try to get better.

0
source

All Articles