[java]代码库
1.主程序
package s1210Arraylist队列;
public class Arraylist implements List {
private Object[] data = new Object[0];
public void add(Object x) {
Object[] data2 = new Object[data.length + 1];
if (data.length >= 1) {
for (int i = 0; i < data.length; i++) {
data2[i] = data[i];
}
}
data2[data.length] = x;
data = data2;
}
public Object remove(int y) {
if (y < 0 || y >= data.length) {
System.out.println("数组越界了");
return null;
}
else {
Object[] data2 = new Object[data.length - 1];
for (int i = 0; i < y; i++)
data2[i] = data[i];
for (int i = y + 1; i < data.length; i++)
data2[i - 1] = data[i];
Object t;
t = data[y];
data = data2;
return t;
}
}
public void insert(Object x, int y) {
if (y < 0 || y > data.length) {
System.out.println("数组越界了");
} else {
Object[] data2 = new Object[data.length + 1];
data2[y - 1] = x;
for (int i = 0; i < y - 1; i++)
data2[i] = data[i];
for (int i = y - 1; i < data.length; i++)
data2[i + 1] = data[i];
data = data2;
}
}
public Object get(int x) {
return data[x];
}
public int size() {
return data.length;
}
}
2.List接口
package s1210Arraylist队列;
public interface List {
public void add(Object x);
public Object remove(int x);
public void insert(Object x,int y);
public int size();
public Object get(int x);
}
3.测试类
package s1210Arraylist队列;
public class Text {
public static void main(String[] args) {
List s=new Arraylist();
for(int i=0;i<10;i++)
{ s.add("str"+i);}
System.out.println();
s.insert("str11",3);
System.out.println("size="+s.size());
for(int i=0;i<s.size();i++)
{System.out.println(s.get(i));}
}
}