java使用顺序表制作电子通讯录,实现添加,删除和排序的功能.

2025年01月05日 11:58
有3个网友回答
网友(1):

翻了半天找到以前写的,只写了部分,你可以参考一下
public class Data {
private long stuNumber;
private String stuName;
private long stuScore;

public Data(){}

public Data(long stuNumber,String stuName,long stuScore){
this.stuNumber=stuNumber;
this.stuName=stuName;
this.stuScore=stuScore;
}

public void setStuNumber(long stuNumber){this.stuNumber=stuNumber;}
public void setStuName(String stuName){this.stuName=stuName;}
public void setStuScore(long stuScore){this.stuScore=stuScore;}

public long getStuNumber(){return stuNumber;}
public String getStuName(){return stuName;}
public long getStuScore(){return stuScore;}
}

public interface LinkListIntf {
public void initList();//初始化
public int size();//获得长度
public void insertData(int n,Data listdata);
public void getData(int n);
public void deleteData(int n);
public void printList();
}

public class ListTest {
public static void main(String[] args) {
int i;
Sqlist sq = new Sqlist();
Data pdata;

System.out.println("--------Start----------");

System.out.println("--------Init------------");
sq.initList();
System.out.println("--------Init Over--------");

Scanner sc = new Scanner(System.in);
System.out.println("输入元素:");
for (int m = 0; m < 2; m++) {
Data data = new Data();
System.out.print("学号 姓名 分数:");
data.setStuNumber(sc.nextLong());
data.setStuName(sc.next());
data.setStuScore(sc.nextLong());
sq.insertData(sq.size()+1, data);
}
while (true) {
System.out.println("----请选择操作------");
System.out.print("1--插入;2--打印;3--删除;4--查找;5--结束");
int choose = sc.nextInt();
switch (choose){
case 1:
System.out.print("插入位置:");
int n = sc.nextInt();

Data data = new Data();
System.out.print("学号 姓名 分数:");
data.setStuNumber(sc.nextLong());
data.setStuName(sc.next());
data.setStuScore(sc.nextLong());
sq.insertData(n,data);
break;
case 2:
System.out.print("打印结果: ");
sq.printList();
break;
case 3:
System.out.print("请输入要删除的元素的位置: ");
int deleteLocation = sc.nextInt();
sq.deleteData(deleteLocation);
break;
case 4:
System.out.print("请输入要查询的元素的位置: ");
int findLocation = sc.nextInt();
System.out.print("元素内容是: ");
sq.getData(findLocation);
break;
case 5:
return;
}

}
}
}

public class Sqlist implements LinkListIntf{
public static final int MAX_LEN = 100;
public int listLength;

private Data[] listdata = new Data[MAX_LEN];

//初始化顺序表
public void initList(){this.listLength=0;}

public int size(){return this.listLength;}

public void insertData(int n,Data data){
int i;
if(listLength>MAX_LEN)
System.out.println("表满!");
if(n<1 || n>listLength+1)
System.out.println("插入位置越界!");

for(i=listLength-1;i>=n-1;i--)
listdata[i+1]=listdata[i];
listdata[n-1]=data;
listLength++;
return;
}

public void deleteData(int n){
if(n<1 || n>listLength-1){System.out.println("删除位置不正确!");return;}
for(int i=n;i listdata[i-1]=listdata[i];
listLength--;
System.out.println("----删除成功!-----");
return;
}

public void getData(int n){
if(n<1 || n>listLength){System.out.println("查找不正确!");}
System.out.println(listdata[n-1].getStuNumber()+" "+listdata[n-1].getStuName()+" "+listdata[n-1].getStuScore());
//return 0;
}

public void printList(){
int i;
System.out.println("表中信息为:");
for(i=0;i String format = String.format(listdata[i].getStuName()+" "+listdata[i].getStuNumber()+" "+listdata[i].getStuScore());
System.out.println(format);
}
//return 0;
}

}

网友(2):

SortedSeqList.java
public class SortedSeqList {

private int MAX_SIZE = 10;

private int[] ary = new int[MAX_SIZE];

private int length = 0;

public SortedSeqList(int[] array) {
if (array == null || array.length == 0) {
this.length = 0;
} else {
ary = array;
length = array.length;
}
}

public void clear() {
length = 0;
}

public boolean isEmpty() {
return length == 0;
}

public void delete(int index) throws Exception {
if (length == 0) {
throw new Exception("No elment to delete");
}

int newAry[] = new int[ary.length - 1];

for (int i = 0, j = 0; i < ary.length; i++) {
if (i == index) {
continue;
} else {
newAry[j++] = ary[i];
}
}

ary = newAry;
length--;
}

public int insert(int value) throws Exception {
if (length == MAX_SIZE) {
throw new Exception("List is full, can't insert more");
}

int[] newAry = new int[length + 1];

int i = 0, j = 0;
for (; i < ary.length; i++, j++) {
if (ary[i] >= value) {
newAry[j] = value;
break;
} else {
newAry[j] = ary[i];
}
}

while (i < ary.length) {
newAry[++j] = ary[i];
i++;
}

ary = newAry;
length++;

return value;

}

public void display() {
System.out.println("\nList now is: ");
for (int i = 0; i < ary.length; i++) {
System.out.print(ary[i] + "\t");
}
}

}

--------------------------SortedSeqList_ex.java
public class SortedSeqList_ex {

public static void main(String[] args) throws Exception {

int[] ary = {1, 2, 3, 5, 7};

SortedSeqList list = new SortedSeqList(ary);
list.display();
list.insert(4);
list.display();
list.delete(2);
list.display();
}

}
---------------testing result

List now is:
1 2 3 5 7
List now is:
1 2 3 4 5 7
List now is:
1 2 4 5 7

网友(3):

我知道你是电子科大成都学院软测的学生