代码拉取完成,页面将自动刷新
package cc.openhome;
import java.util.Arrays;
import java.util.concurrent.locks.*;
public class ArrayList2<E> {
private ReadWriteLock lock = new ReentrantReadWriteLock();
private Object[] elems;
private int next;
public ArrayList2(int capacity) {
elems = new Object[capacity];
}
public ArrayList2() {
this(16);
}
public void add(E elem) {
lock.writeLock().lock();
try {
if (next == elems.length) {
elems = Arrays.copyOf(elems, elems.length * 2);
}
elems[next++] = elem;
} finally {
lock.writeLock().unlock();
}
}
public E get(int index) {
lock.readLock().lock();
try {
return (E) elems[index];
} finally {
lock.readLock().unlock();
}
}
public int size() {
lock.readLock().lock();
try {
return next;
} finally {
lock.readLock().unlock();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。