package utils;
public class MyArrayList<T> {
private static final int DEFAULT_CAPACITY = 50;
private int theSize;
private Object[] theItems;
public MyArrayList() {
clear();
}
public void clear() {
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
public int size() {
return theSize;
}
public boolean isEmpty() {
return size() == 0;
}
public void trimToSize() {
ensureCapacity(size());
}
public T get(int idx) {
if (idx < 0 || idx >= size())
return null;// should throw exception
return (T)theItems[idx];
}
public T get(T x) {
for (int i = 0; i < size(); ++i) {
if (theItems[i].equals(x)) {
return (T)theItems[i];
}
}
return null;
}
public int indexOf(T x) {
for (int i = 0; i < size(); ++i) {
if (theItems[i].equals(x)) {
return i;
}
}
return -1;
}
public T set(int idx, T newVal) {
if (idx < 0 || idx
为算法考试做准备--ArrayList实现
最新推荐文章于 2022-10-05 10:52:17 发布