A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 林林鸦 初级黑马   /  2014-4-5 12:32  /  731 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 林林鸦 于 2014-4-7 10:48 编辑
  1. import java.util.*;

  2. public class CountingIntegerList extends AbstractList<Integer> {
  3.         private int size;

  4.         public CountingIntegerList(int size) {
  5.                 this.size = size < 0 ? 0 : size;
  6.         }
  7.         
  8.         public Integer get(int index) {
  9.                 return Integer.valueOf(index);
  10.         }

  11.         public int size() {
  12.                 return size;
  13.         }

  14.         public static void main(String[] args) {
  15.                 System.out.println(new CountingIntegerList(30));
  16.         }
  17. } /*
  18. * Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  19. * 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
  20. */
复制代码
为什么会产生上面的输出结果

1 个回复

倒序浏览
是因为他的父类中 有一个toString方法,已经复写了Object的toString
toString
public String toString()返回此 collection 的字符串表示形式。该字符串表示形式由 collection 元素的列表组成,这些元素按其迭代器返回的顺序排列,并用方括号 ("[]") 括起来。相邻元素由字符 ", "(逗号加空格)分隔。通过 String.valueOf(Object) 可以将元素转换成字符串。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马