就插入方法烧了一点时间,有兴趣可以研究研究。
//接口里面还可自添 排序等方法
interface IList {
void Print_List();// 打印数组
void Length_List();// 有效数据长度
void Add_List(int data);// 向数组添加数据
int Locate_List(int data);// 按值查询
int Delete_List(int data);// 删除数据
int Update_List(int Olddata, int Newdata);// 修改数据
int preInsert_List(int data, int location);// 按下标插入
}
public class SetList implements IList {
private static final int MAXSIZE = 100;
private static int[] intarray;
private int effectiveData = 0;// 输入有效数据总数
public SetList() {
intarray = new int[MAXSIZE];
}
public static void main(String[] args) {
SetList user = new SetList();
user.Add_List(10);
user.Add_List(15);
user.Add_List(20);
user.Add_List(28);
user.Add_List(30);
user.Add_List(34);
user.Print_List();
// 添加
user.Add_List(40);
user.Print_List();
// 查找
user.Locate_List(30);
// 删除
user.Delete_List(10);
user.Print_List();
// 修改
user.Update_List(34, 38);
user.Print_List();
// 插入
user.preInsert_List(45, 7);
user.Print_List();
//
}
public void Print_List() {
// 输出数组
for (int i = 0; i < effectiveData; i++) {
System.out.print(intarray[i] + " ");
}
System.out.println();
}
public void Length_List() {
System.out.println("共有效数据:" + effectiveData);
}
public void Add_List(int data) {// 添加数据
if (effectiveData == MAXSIZE - 1)
System.out.println("List Full");
intarray[effectiveData] = data;
effectiveData++;
}
public int Locate_List(int data) {// 查找,返回下标
int i;
for (i = 0; i < effectiveData; i++) {
if (intarray[i] == data) {
break;
}
}
if (i == effectiveData) {
System.out.println("the search failed");
} else
System.out.println("下标为:" + i);
return 0;
}
public int Delete_List(int data) {// 删除
int i;
for (i = 0; i < effectiveData; i++) {
if (intarray[i] == data) {
break;
}
}
if (i == effectiveData) {
System.out.println("the search failed");
} else
for (int j = i; j < effectiveData; j++) {
intarray[j] = intarray[j + 1];// 后面数据往前面移动
}
effectiveData--;// 有效数据减一
return 0;
}
public int Update_List(int Olddata, int Newdata) {// 输入原值和修改值
int i;
for (i = 0; i < effectiveData; i++) {
if (intarray[i] == Olddata) {
break;
}
}
if (i == effectiveData) {
System.out.println("the search failed");
} else
intarray[i] = Newdata;
return 0;
}
public int preInsert_List(int data, int location) {
if (effectiveData == MAXSIZE - 1) {
System.out.println("List Full");
return 0;
}
if (location < 0 || location >= effectiveData + 1) {
System.out.println("Fatal Error");
return 0;
}
for (int j = effectiveData; j > location - 1; j--) {
intarray[j + 1] = intarray[j];
}
// 赋值到输入下标位置
intarray[location] = data;
effectiveData++;// 添加成功自加
return 0;
}
}