AbstractCollection源代码的一个小问题
我在看AbstractCollection的源代码时,看到这么一个方法:
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("[");
Iterator<E> i = iterator();
boolean hasNext = i.hasNext(); //为什么不直接用i.hasNext? 这样做有什么好处么?还是有其他原因?
while (hasNext) {
E o = i.next();
buf.append(o == this ? "(this Collection)" : String.valueOf(o));
hasNext = i.hasNext();
if (hasNext) //这边也是
buf.append(", ");
}
buf.append("]");
return buf.toString();
}