java使用顺序表制作电子通讯录,实现添加,删除和排序的功能. 10
菜鸟一只总做不出来,求高手指点~在线等T_T最好详细代码!!1.添加联系人信息2删除联系人信息3查看联系人信息4退出---------------------------...
菜鸟一只总做不出来,求高手指点~在线等T_T 最好详细代码!!
1.添加联系人信息
2删除联系人信息
3查看联系人信息
4退出
--------------------------------------------------------------------------------------------
当输入1时,以进入添加联系人信息,添加的信息包括姓名和电话号码
当输入2时,可进入删除联系人信息,通过输入联系人姓名完成删除。
当输入3时,可进行查看,通过输入联系人姓名,查看其他电话号码。
当输入4时,关闭程序。 展开
1.添加联系人信息
2删除联系人信息
3查看联系人信息
4退出
--------------------------------------------------------------------------------------------
当输入1时,以进入添加联系人信息,添加的信息包括姓名和电话号码
当输入2时,可进入删除联系人信息,通过输入联系人姓名完成删除。
当输入3时,可进行查看,通过输入联系人姓名,查看其他电话号码。
当输入4时,关闭程序。 展开
3个回答
展开全部
翻了半天找到以前写的,只写了部分,你可以参考一下
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<listLength;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<listLength;i++) {
String format = String.format(listdata[i].getStuName()+" "+listdata[i].getStuNumber()+" "+listdata[i].getStuScore());
System.out.println(format);
}
//return 0;
}
}
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<listLength;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<listLength;i++) {
String format = String.format(listdata[i].getStuName()+" "+listdata[i].getStuNumber()+" "+listdata[i].getStuScore());
System.out.println(format);
}
//return 0;
}
}
展开全部
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
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
追问
别用复制的可好
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我知道你是电子科大成都学院软测的学生
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询