[英]What is the bug in this code
我正在閱讀測試的重要性,並以示例代碼的形式出現:
public Scheme join(final Scheme other) {
final HashSet<Attribute> $ = new HashSet<Attribute>();
for (int i = 0; i < attributes.length; ++i)
$.add(attributes[i]);
for (int i = 0; i < attributes.length; ++i)
$.add(other.attributes[i]);
return new Scheme(true, $.toArray(new Attribute[$.size()]));
}
它說它在Scehme.join()中有一個錯誤,但是我看不到!
該錯誤在您的第二個循環中:
// Here ------------v
for (int i = 0; i < attributes.length; ++i)
$.add(other.attributes[i]);
那應該是other.attributes
,而不是attributes
。
這是一個很好的例子,說明為什么使用增強的for
循環是個好主意:
public Scheme join(final Scheme other) {
final HashSet<Attribute> $ = new HashSet<Attribute>();
for (Attribute attr : attributes) {
$.add(attr);
}
for (Attribute attr : other.attributes) {
$.add(attr);
}
return new Scheme(true, $.toArray(new Attribute[$.size()]));
}
我的猜測將在這里
final HashSet<attribute> $ = new HashSet<Attribute>();
左邊是attribute
,右邊是Attribute
,並且因為Java是區分大小寫的語言,所以這是兩個不同的對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.