1700444240
1700444241
c2是通过subList方法从c列表中生成的一个子列表,然后c2又增加了一个元素,可为什么增加了一个元素还会相等呢?我们来看subList源码:
1700444242
1700444243
public List<E>subList(int fromIndex, int toIndex){
1700444244
1700444245
return(this instanceof RandomAccess?
1700444246
1700444247
new RandomAccessSubList<E>(this, fromIndex, toIndex):
1700444248
1700444249
new SubList<E>(this, fromIndex, toIndex));
1700444250
1700444251
}
1700444252
1700444253
subList方法是由AbstractList实现的,它会根据是不是可以随机存取来提供不同的SubList实现方式,不过,随机存储的使用频率比较高,而且RandomAccessSubList也是SubList子类,所以所有的操作都是由SubList类实现的(除了自身的SubList方法外),那么,我们就直接来看SubList类的代码:
1700444254
1700444255
class SubList<E>extends AbstractList<E>{
1700444256
1700444257
//原始列表
1700444258
1700444259
private AbstractList<E>l;
1700444260
1700444261
//偏移量
1700444262
1700444263
private int offset;
1700444264
1700444265
//构造函数,注意list参数就是我们的原始列表
1700444266
1700444267
SubList(AbstractList<E>list, int fromIndex, int toIndex){
1700444268
1700444269
/*下标校验,省略*/
1700444270
1700444271
//传递原始列表
1700444272
1700444273
l=list;
1700444274
1700444275
offset=fromIndex;
1700444276
1700444277
//子列表的长度
1700444278
1700444279
size=toIndex-fromIndex;
1700444280
1700444281
}
1700444282
1700444283
//获得指定位置的元素
1700444284
1700444285
public E get(int index){
1700444286
1700444287
/*校验部分,省略*/
1700444288
1700444289
//从原始字符串中获得指定位置的元素
[
上一页 ]
[ :1.70044424e+09 ]
[
下一页 ]