/** * Returns {@code true} if this list contains the specified element. * More formally, returns {@code true} if and only if this list contains * at least one element {@code e} such that * {@code Objects.equals(o, e)}. * * @param o element whose presence in this list is to be tested * @return {@code true} if this list contains the specified element */ publicbooleancontains(Object o) { return indexOf(o) >= 0; }
/** * Returns the index of the first occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the lowest index {@code i} such that * {@code Objects.equals(o, get(i))}, * or -1 if there is no such index. */ publicintindexOf(Object o) { return indexOfRange(o, 0, size); }
intindexOfRange(Object o, int start, int end) { Object[] es = elementData; if (o == null) { for (inti= start; i < end; i++) { if (es[i] == null) { return i; } } } else { for (inti= start; i < end; i++) { if (o.equals(es[i])) { return i; } } } return -1; }
/** * Returns the index of the last occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the highest index {@code i} such that * {@code Objects.equals(o, get(i))}, * or -1 if there is no such index. */ publicintlastIndexOf(Object o) { return lastIndexOfRange(o, 0, size); }
intlastIndexOfRange(Object o, int start, int end) { Object[] es = elementData; if (o == null) { for (inti= end - 1; i >= start; i--) { if (es[i] == null) { return i; } } } else { for (inti= end - 1; i >= start; i--) { if (o.equals(es[i])) { return i; } } } return -1; }