| public class Student { |
| |
| String name; |
| |
| String sex; |
| |
| int sno; |
| |
| int score; |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public String getSex() { |
| return sex; |
| } |
| |
| public void setSex(String sex) { |
| this.sex = sex; |
| } |
| |
| public int getSno() { |
| return sno; |
| } |
| |
| public void setSno(int sno) { |
| this.sno = sno; |
| } |
| |
| public int getScore() { |
| return score; |
| } |
| |
| public void setScore(int score) { |
| this.score = score; |
| } |
| |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", sex='" + sex + '\'' + |
| ", sno=" + sno + |
| ", score=" + score + |
| '}'; |
| } |
| |
| } |
| public class MyArrar { |
| |
| Student[] num = new Student[100]; |
| |
| private int len=0; |
| |
| public int size(){ |
| return len; |
| } |
| |
| |
| public int add(Student stu){ |
| if(len<num.length){ |
| num[len]=stu; |
| len++; |
| return 1; |
| } else { |
| return -1; |
| } |
| } |
| |
| |
| public void show(){ |
| System.out.println("学号\t姓名\t性别\t成绩"); |
| for (int i = 0; i < len; i++) { |
| Student stu=num[i]; |
| System.out.println(" " + stu.getSno() + " " + stu.getName() |
| + " " + stu.getSex() + " " + stu.getScore()); |
| } |
| } |
| |
| |
| |
| public int remove(int index){ |
| if(index>len-1){ |
| return -1; |
| }else { |
| for(int i=index; i<len; i++){ |
| num[i]=num[i+1]; |
| } |
| len--; |
| return 1; |
| } |
| } |
| |
| |
| |
| public int removeSno(int sno){ |
| try { |
| int index = 0; |
| for(int i=0; i<len; i++){ |
| if(num[i].getSno()==sno){ |
| index=i; |
| break; |
| } |
| } |
| |
| for(int i=index; i<len; i++){ |
| num[i]=num[i+1]; |
| } |
| len--; |
| return 1; |
| }catch (ArrayIndexOutOfBoundsException e){ |
| return -1; |
| } |
| } |
| |
| |
| public Student findBySno(int sno) { |
| int index = 0; |
| for(int i=0; i<len; i++){ |
| if(num[i].getSno()==sno){ |
| index=i; |
| break; |
| } |
| } |
| return num[index]; |
| } |
| |
| |
| public Student findByName(String name){ |
| int index = 0; |
| for(int i=0; i<len; i++){ |
| if(num[i].getName()==name){ |
| index=i; |
| break; |
| } |
| } |
| return num[index]; |
| } |
| |
| |
| public void sortAsc(){ |
| for(int b = len-1; b>0; b--){ |
| Student tmp; |
| int i=0; |
| for(int a = b; a>0; a--){ |
| if(num[i].getScore()>num[i+1].getScore()){ |
| tmp = num[i]; |
| num[i] = num[i+1]; |
| num[i+1] = tmp; |
| } |
| i++; |
| } |
| } |
| } |
| |
| |
| public void addSortAsc(Student stu){ |
| for (int i = 0; i < len; i++) { |
| if (stu.getScore() < num[i].getScore()){ |
| |
| for(int j=len; j>i; j--){ |
| num[j]=num[j-1]; |
| } |
| |
| len++; |
| |
| num[i]=stu; |
| |
| break; |
| }else { |
| |
| continue; |
| } |
| } |
| |
| if(stu.getScore() >= num[len-1].getScore()){ |
| num[len]=stu; |
| len++; |
| } |
| } |
| |
| |
| public void sortDesc(){ |
| for(int b = len-1; b>0; b--){ |
| Student tmp; |
| int i=0; |
| for(int a = b; a>0; a--){ |
| if(num[i].getScore()<num[i+1].getScore()){ |
| tmp = num[i]; |
| num[i] = num[i+1]; |
| num[i+1] = tmp; |
| } |
| i++; |
| } |
| } |
| } |
| |
| |
| |
| |
| public void addSortDesc(Student stu){ |
| for (int i = 0; i < len; i++) { |
| if (stu.getScore() > num[i].getScore()){ |
| |
| for(int j=len; j>i; j--){ |
| num[j]=num[j-1]; |
| } |
| |
| len++; |
| |
| num[i]=stu; |
| |
| break; |
| }else { |
| |
| continue; |
| } |
| } |
| |
| if(stu.getScore() <= num[len-1].getScore()){ |
| num[len]=stu; |
| len++; |
| } |
| } |
| |
| } |
| public class test { |
| public static void main(String[] args) { |
| MyArrar myArrar=new MyArrar(); |
| |
| Student student=new Student(); |
| student.setName("张三"); |
| student.setSex("男"); |
| student.setSno(3); |
| student.setScore(68); |
| myArrar.add(student); |
| Student student1=new Student(); |
| student1.setName("张四"); |
| student1.setSex("女"); |
| student1.setSno(9); |
| student1.setScore(95); |
| myArrar.add(student1); |
| Student student2=new Student(); |
| student2.setName("李三"); |
| student2.setSex("男"); |
| student2.setSno(6); |
| student2.setScore(72); |
| myArrar.add(student2); |
| |
| System.out.println(myArrar.size()); |
| |
| myArrar.show(); |
| |
| |
| |
| |
| |
| |
| |
| |
| System.out.println(myArrar.findBySno(6)); |
| System.out.println(myArrar.findByName("李三")); |
| |
| |
| System.out.println("--- 成绩升序 ---"); |
| myArrar.sortAsc(); |
| myArrar.show(); |
| System.out.println("--- 插入一条数据 ---"); |
| Student student3=new Student(); |
| student3.setName("李五"); |
| student3.setSex("男"); |
| student3.setSno(7); |
| student3.setScore(95); |
| myArrar.addSortAsc(student3); |
| myArrar.show(); |
| |
| System.out.println("--- 成绩降序 ---"); |
| myArrar.sortDesc(); |
| myArrar.show(); |
| System.out.println("--- 插入一条数据 ---"); |
| Student student4=new Student(); |
| student4.setName("李六"); |
| student4.setSex("男"); |
| student4.setSno(2); |
| student4.setScore(68); |
| myArrar.addSortDesc(student4); |
| myArrar.show(); |
| |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2022-09-29 马踏棋盘算法
2022-09-29 弗洛伊德算法
2022-09-29 迪杰斯特拉算法