ArrayList源码(改)(查)
- 改
// 传入更改元素的位置和内容,将被更改的元素传出 public E set(int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; }
- 查
// 查询某元素是否存在 public boolean contains(Object o) { return indexOf(o) >= 0; } // 查询某个元素位置正向 public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } // 查询某个元素位置反向 public int lastIndexOf(Object o) { if (o == null) { for (int i = size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; }
// 获取某个未知的元素 public E get(int index) { rangeCheck(index); return elementData(index); } E elementData(int index) { return (E) elementData[index]; }
相关推荐
源码物语 2020-07-18
xiaouncle 2020-07-31
XGQ 2020-06-21
fengyun 2020-06-14
OldBowl 2020-06-11
zagnix 2020-06-04
xhao 2020-06-04
付春杰Blog 2020-05-31
ilovewqf 2020-05-30
shenxiuwen 2020-05-29
XGQ 2020-05-26
zhuyonge 2020-05-09
zcpHappy 2020-04-30
luohui 2020-04-29
fraternityjava 2020-04-29
willluckysmile 2020-04-24
shayuchaor 2020-04-20
fengyun 2020-04-17