泛型(模拟list)

package Test;

import java.util.Arrays;

public class genericity<T> {
private Object[] t;

public genericity() {
super();
t = new Object[0];
}

//增加数据

public void add(T t1) {
t = Arrays.copyOf(t, t.length + 1);
t[t.length - 1] = t1;
}

//查询数据

public int queryAll(T t1) {
for (int i = 0; i < t.length; i++) {
if (t[i] == t1) {
return i;
}
}
return -1;
}

//删除数据

public void remove(T t1) {
int count = queryAll(t1);
Object[] o = t;
t = new Object[o.length - 1];
int a = 0;
for (int i = 0; i < o.length; i++) {
if (i != count) {
t[a++] = o[i];
}
}
}

//修改数据

public void update(T t1, T t2) {
t[queryAll(t1)] = t2;
}

//查询所有数据

public void show() {
for (int i = 0; i < t.length; i++) {
System.out.println(t[i]);
}
}
}

posted on 2017-03-03 15:26  Joyous丶  阅读(209)  评论(0编辑  收藏  举报

导航