java 简单 基础 问题

[复制链接]
查看11 | 回复2 | 2010-7-25 14:59:57 | 显示全部楼层 |阅读模式
List[I] list1 = Arrays.asList(1,2,3,4,5);
// list1.add(6); why ..it will cause a runtime error???
书上写的不明白:
it's also possible to use the output of Arrays.asList() directly, as
a List, but the underlying representation in this case in the array, which cannot be resized.
请问,,是怎么变成数组的呢

回复

使用道具 举报

千问 | 2010-7-25 14:59:57 | 显示全部楼层
我觉得吧,Arrays.asList()返回的类型可以转型为List,但是底层是数组,所以不能调整尺寸,至于Arrays.asList()怎么把这个数组变出来的,就更不清楚了=.=Arrays.asList()返回的类型是Arrays$ArrayList,Arrays$ArrayList的基类是AbstractList,而AbstractList实现了List接口,于是...
回复

使用道具 举报

千问 | 2010-7-25 14:59:57 | 显示全部楼层
使用Collection.toArray()即可变为数组了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行