[英]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.