为什么ArrayList直接输出不是一个内存地址,而是数组元素...
你看下ArrayList ToString()的源码,是不是重写了ToString() ArrayList 后面是string 类型的啊。输出不就是string 类型的吗.... /**
* Returns a string representation of this collection.The string
* representation consists of a list of the collection's elements in the
* order they are returned by its iterator, enclosed in square brackets
* (<tt>"[]"</tt>).Adjacent elements are separated by the characters
* <tt>", "</tt> (comma and space).Elements are converted to strings as
* by {@link String#valueOf(Object)}.
*
* @return a string representation of this collection
*/
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
} 你可以直接输出的是这个类覆盖了toString方法,它打印出了地址这个类覆盖了toString方法, 要打印一个对象,我们调用对象本身的toString方法。默认的toString方法是输出地址。一些类重写toString方法来输出内容。ArrayList覆盖toString方法,以便输出ArrayList的内容 我想他想说的是打印出数组为什么它是一个内存地址
如果想直接打印Arrays,可以使用Arrays. tostring (arr)
页:
[1]