jaVA的vector的源码_java1.7集合源码阅读: Vector

1 /**

2 * Returns {@codetrue} if this vector contains the specified element.3 * More formally, returns {@codetrue} if and only if this vector4 * contains at least one element {@codee} such that5 * (o==null ? e==null : o.equals(e)).6 *7 *@paramo element whose presence in this vector is to be tested8 *@return{@codetrue} if this vector contains the specified element9 */

10 public booleancontains(Object o) {11 return indexOf(o, 0) >= 0;12 }13

14 /**

15 * Returns the index of the first occurrence of the specified element16 * in this vector, or -1 if this vector does not contain the element.17 * More formally, returns the lowest index {@codei} such that18 * (o==null ? get(i)==null : o.equals(get(i))),19 * or -1 if there is no such index.20 *21 *@paramo element to search for22 *@returnthe index of the first occurrence of the specified element in23 * this vector, or -1 if this vector does not contain the element24 */

25 public intindexOf(Object o) {26 return indexOf(o, 0);27 }28

29 /**

30 * Returns the index of the first occurrence of the specified element in31 * this vector, searching forwards from {@codeindex}, or returns -1 if32 * the element is not found.33 * More formally, returns the lowest index {@codei} such that34 * (i >= index && (o==null ? get(i)==null : o.equals(get(i)))),35 * or -1 if there is no such index.36 *37 *@paramo element to search for38 *@paramindex index to start searching from39 *@returnthe index of the first occurrence of the element in40 * this vector at position {@codeindex} or later in the vector;41 * {@code-1} if the element is not found.42 *@throwsIndexOutOfBoundsException if the specified index is negative43 *@seeObject#equals(Object)44 */

45 public synchronized int indexOf(Object o, intindex) {46 if (o == null) {47 for (int i = index ; i < elementCount ; i++)48 if (elementData[i]==null)49 returni;50 } else{51 for (int i = index ; i < elementCount ; i++)52 if(o.equals(elementData[i]))53 returni;54 }55 return -1;56 }57

58 /**

59 * Returns the index of the last occurrence of the specified element60 * in this vector, or -1 if this vector does not contain the element.61 * More formally, returns the highest index {@codei} such that62 * (o==null ? get(i)==null : o.equals(get(i))),63 * or -1 if there is no such index.64 *65 *@paramo element to search for66 *@returnthe index of the last occurrence of the specified element in67 * this vector, or -1 if this vector does not contain the element68 */

69 public synchronized intlastIndexOf(Object o) {70 return lastIndexOf(o, elementCount-1);71 }72

73 /**

74 * Returns the index of the last occurrence of the specified element in75 * this vector, searching backwards from {@codeindex}, or returns -1 if76 * the element is not found.77 * More formally, returns the highest index {@codei} such that78 * (i <= index && (o==null ? get(i)==null : o.equals(get(i)))),79 * or -1 if there is no such index.80 *81 *@paramo element to search for82 *@paramindex index to start searching backwards from83 *@returnthe index of the last occurrence of the element at position84 * less than or equal to {@codeindex} in this vector;85 * -1 if the element is not found.86 *@throwsIndexOutOfBoundsException if the specified index is greater87 * than or equal to the current size of this vector88 */

89 public synchronized int lastIndexOf(Object o, intindex) {90 if (index >=elementCount)91 throw new IndexOutOfBoundsException(index + " >= "+elementCount);92

93 if (o == null) {94 for (int i = index; i >= 0; i--)95 if (elementData[i]==null)96 returni;97 } else{98 for (int i = index; i >= 0; i--)99 if(o.equals(elementData[i]))100 returni;101 }102 return -1;103 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值