destination.clear();
for (T o : source) {
destination.add(o);
}
}
public static %26lt;T%26gt; boolean compareSets(Set%26lt;T%26gt; a, Set%26lt;T%26gt; b) {
if (a.size() != b.size()) {
return false;
}
Iterator%26lt;T%26gt; iteratorA = a.iterator();
Iterator%26lt;T%26gt; iteratorB = b.iterator();
while (iteratorA.hasNext()) {
if (iteratorA.next() != iteratorB.next()) {
return false;
}
}
return true;
}
public static void main(String [] args) {
Set%26lt;Integer%26gt; a, b;
...
copySet(a, b);
System.out.println(';compareSets(a, b); '; + compareSets(a, b));
}Explain how the print statement could print 'false'?
sets aren't ordered.
If your original set (a) looked like: { 1, 2, 3 };
and you called
copySet(a, b);
b could look like { 2, 3, 1 }
I also notice that interatorB never has .hasNext called. Pretty sure this'll throw an exception.
No comments:
Post a Comment