当 public class aa extends ArrayList<Integer>{ }时
Foreach循环是自动调用 iterator()的,那么如果 System.out.println(new aa());是否也会自动调用呢?这又是为什么呢?
Foreach循环是自动调用 iterator()的,那么如果 System.out.println(new aa());是否也会自动调用呢?这又是为什么呢?
解决方案 »
- 新人请教用java做麻将思路!
- java如何实现广域网通信?
- JList 数组方式创建
- Runtime.getRuntime().exec()可否依次执行多条命令?
- javax.swing 里面的键盘事件怎么用啊??????????????????
- unicode字符转换成中文问题(仍未解决)
- java中怎样调用C++写的DLL啊?
- 用poi把excel 导入sqlserver 问题!请各位指教!
- !急啊 立刻给分!weblogic.ant.taskdefs.ejb.DDInit生成xml时出错信息!什么原因?
- 上海很水的JAVA公司
- 算法统计,在线等,急!!!!!!!!!!!!!!!!
- 数据类型的计算问题
你这里没有foreach,也没有iterator,不会调用
extends AbstractList<Integer> {
private int size;
public CountingIntegerList(int size) {
this.size = size < 0 ? 0 : size;
}
public Integer get(int index) {
return Integer.valueOf(index);
}
public int size() { return size; }
public static void main(String[] args) {
System.out.println(new CountingIntegerList(30));
}
} /* Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
*///:~
顺便问下,如果 iterator()被调用了,那么我又定义了 hasNext() 和 next() 那么这两个方法会被自动调用吗?
StringBuffer buf = new StringBuffer();
buf.append("["); Iterator<E> i = iterator();
boolean 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();
}
你单步跟踪一下就知道了.
System.out.println()打印的是一个对象引用时,自动调用其toString()方法