问题:来源于码农翻身公众号
也不知道可以不,看看就好hhhhh
/**
* 自定义的一个模仿ArrayList的类, 你需要实现其中的add, get, remove , 等方法
* @author 刘欣
*/
public class SimpleList<T>{
private Object[] elementData;
private int size=0;
public int size() {
return -1;
}
public SimpleList(){
}
public boolean isEmpty() {
return false;
}
public boolean add(T e) {
return false;
}
public boolean remove(Object o) {
return false;
}
public T get(int index) {
return null;
}
}
SimpleList.java remove(Object o) 改成 remove(int index)
package helloworld;
import java.util.Arrays;
public class SimpleList<T> {
private Object[] elementData;
private int size = 0;
private int initsize = 2;
public int size() {
return size;
}
public SimpleList() {
elementData = new Object[initsize];
elementData.toString();
}
public boolean isEmpty() {
return (this.size > 0) ? false : true;
}
public boolean add(T e) {
if (size >= (elementData.length - 1)) {
Object[] a = new Object[1 + elementData.length];
a.toString();
System.arraycopy(elementData, 0, a, 0, elementData.length);
elementData = a;
}
elementData[size++] = e;
return true;
}
public boolean remove(int index) {
for (int i = index; i < size; i++) {
elementData[i] = elementData[index + 1];
}
elementData[size] = 0;
size--;
return true;
}
public T get(int index) {
return (T) elementData[index];
}
@Override
public String toString() {
return "SimpleList [elementData=" + Arrays.toString(elementData) + ", size=" + size + "]";
}
}
调用 uselist.java
package helloworld;
public class uselist {
public static void main(String[] args) {
SimpleList<Integer> simarr = new SimpleList<Integer>();
System.out.println("simarr.isEmpty():" + simarr.isEmpty() + " simarr.size():" + simarr.size());
simarr.add(97);
simarr.add(56);
simarr.add(16);
System.out.println("simarr.isEmpty():" + simarr.isEmpty() + " simarr.size():" + simarr.size()
+ " simarr.get(1):" + simarr.get(0));
simarr.remove(0);
System.out.println("simarr.isEmpty():" + simarr.isEmpty() + " simarr.size():" + simarr.size()
+ " simarr.get(1):" + simarr.get(0));
}
}