Demo entry 6364431

12123123

   

Submitted by anonymous on May 16, 2017 at 11:11
Language: Java. Code size: 648 Bytes.

/**
 *  这是一个跟`indexOf(Object oj)`类似的方法,实现原理一样,只不过是从后向前遍历数组
 */
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 int size() {
    return size;
}

public void clear() {
    modCount++;
    // clear to let GC do its work
    for (int i = 0; i < size; i++)
        elementData[i] = null;
    size = 0;
}

public boolean isEmpty() {
    return size == 0;
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).