import java.util.ArrayList;
public class MyArrayList {
private Object value[];
private int size;
public MyArrayList() {
this(2);
}
public MyArrayList(int size) {
value = new Object[size];
}
public void add(Object obj) {
value[size] = obj;
size++;
if (size >= value.length) {
int newCapacity = value.length * 2;
Object[] newList = new Object[newCapacity];
for (int i = 0; i < value.length; i++) {
newList[i] = value[i];
}
value = newList;
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public int indexOf(Object obj) {
if (obj == null) {
return -1;
} else {
for (int i = 0; i < value.length; i++) {
if (obj == value[i]) {
return i;
}
}
return -1;
}
}
public int lastIndexOf(Object obj) {
if (obj == null) {
return -1;
} else {
for (int i = value.length - 1; i >= 0; i--) {
if (obj == value[i]) {
return i;
}
}
return -1;
}
}
public void rangleCheck(int index) {
if (index < 0 || index > size - 1) {
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public Object set(int index, Object object) {
rangleCheck(index);
Object old = value[index];
value[index] = object;
return old;
}
public Object get(int index) {
rangleCheck(index);
return value[index];
}
public static void main(String[] args) {
MyArrayList list = new MyArrayList();
list.add("aaa");
list.add(new Human("高淇"));
list.add("bbb");
list.add("bbb");
list.add("bbb");
list.add("bbb");
Human h = (Human) list.get(1);
System.out.println(h.getName());
System.out.println("*********************");
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.get(2));
System.out.println("*******************");
System.out.println(list.size);
}
}
class Human {
private String name;
public Human(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}