关于List 的get方法

来源:互联网 发布:淘宝钻石等级 编辑:程序博客网 时间:2024/06/07 09:07

看下面一段程序

List<String> list = new ArrayList<String>();System.out.println(list.get(0));

这个会输出什么?

------------------------------

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

下标越界异常。

原因:看了下get的API,

IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
恍然大悟。