java - How Set.contains() decides whether it's a subset or not? -


i expect following code give me subset , complementary set.

but actually, result shows "error: not subset!"

what it.next() , how revise code result want? thanks!

package chapter8;  import java.util.hashset; import java.util.iterator; import java.util.set;  public class 3 {     int n;     set<integer> set = new hashset<integer>();      public static void main(string args[]) {         3 three = new three(10);         three.display(three.set);         set<integer> test = new hashset<integer>();         iterator<integer> = three.set.iterator();         while(it.hasnext()) {             test.add(it.next());             three.display(test);             three.display(three.complementaryset(test));         }      }      boolean contains(set<integer> s) {         if (this.set.contains(s))             return true;         else              return false;     }      set<integer> complementaryset(set<integer> s) {         if(this.set.contains(s)){             set<integer> result = this.set;             result.removeall(s);             return result;         }         else {             system.out.println("error: not subset!");             return null;         }     }      three() {         this.n = 3;         this.randomset();     }      three(int n) {         this.n = n;         this.randomset();     }      void randomset() {         while(set.size() < n) {             set.add((int)(math.random()*10));         }     }      void display(set<integer> s) {         system.out.println("the set " + s.tostring());     } } 

your problem in part:

set.contains(s) 

that doesn't think does, doesn't take argument set see if members contained in firstset. rather looks if argument passed in set.

you need iterate on "contained" set , use set.contains(element) each element in contained set.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -