Thursday, February 4, 2010

Explain how the print statement could print 'false'?

public static %26lt;T%26gt; void copySet(Set%26lt;T%26gt; source, Set%26lt;T%26gt; destination) {


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.
  • computer security
  • No comments:

    Post a Comment