开发者社区> 问答> 正文

JAVA关于顺序表操作的问题

public class StudScore {

    String name;
    int studentNo;
    double score;

    public StudScore() {

    }

    public StudScore(String name, int studentNo, double score) {
        this.name = name;
        this.studentNo = studentNo;
        this.score = score;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setStudentNo(int studentNo) {
        this.studentNo = studentNo;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getStudentNo() {
        return studentNo;
    }

    public double getScore() {
        return score;
    }

    public String toString() {
        return name + "," + studentNo + "," + score;
    }

}
public interface StudOPeration {

    void add(StudScore node);

    void listAll();

    void search(int index);

    int getCounts();

    void delete(int index);

    void search(String name);

}
public class ArrStudent implements StudOPeration {

    StudScore stud[] = new StudScore[5];
    int count = 0;
    String name;

    public ArrStudent() {

    }

    public ArrStudent(int size) {

    }
    @Override
    public void add(StudScore node) {
        int i = count;
        stud[i] = node;
        count++;

        if (count == stud.length) {
            System.out.println("存储空间已满");
        }
    }
    @Override
    public void listAll() {
        for (int i = 0; i < count; i++) {
            System.out.print(stud[i].name + "\t");
            System.out.print(stud[i].studentNo + "\t");
            System.out.println(stud[i].score);
        }
    }
    @Override
    public void search(int index) {
        if (index <= count && index > 0) {
            System.out.println("您查找信息是:" + stud[index - 1]);
        } else {
            System.out.println("输入的序号无效!!");
        }
    }
    @Override
    public int getCounts() {

        return 0;
    }
    @Override
    public void delete(int index) {

    }
    @Override
    public void search(String name) {
    }
}

展开
收起
蛮大人123 2016-06-02 17:06:04 1740 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    @Override
    public int getCounts() {
    System.out.println("现在共有"+count+"个学生");
    return count;
    }
    /**
    *尽量用list
    */
    @Override
    public void delete(int index) {
    System.out.println("删除的学生是: "+stud[index-1].name);
    stud[index-1]=null;
    for(int i=0;i<stud.length-1;i++){
    if(stud[i]==null){
    stud[i]=stud[i++];//为空的后面的对象全部往前移一位
    }
    }
    stud[stud.length-1]=null;//把最后一个赋值为null
    }
    
    @Override
    public void search(String name) {
    for(int i=0;i<stud.length;i++){
    if(name.equals(stud[i].name)){
    System.out.println("您要查询的"+name+"学生在第"+(i+1)+"位");
    }else{
    System.out.println("您要查询的"+name+"学生不存在");
    }
    }
    }
    2019-07-17 19:25:18
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载