先创建一个学生类里面定义三个变量并且重写equals方法和添加toString方法
package 学生管理类; public class syudent { String name; int age; int score; @Override public String toString() { return "syudent [name=" + name + ", age=" + age + ", score=" + score + "]"; } public syudent(String name, int age, int score) { super(); this.name = name; this.age = age; this.score = score; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + score; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; syudent other = (syudent) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (score != other.score) return false; return true; } }在编写四个List集合的基本操作来操作 List集合里面的元素
package 学生管理类; import java.util.ArrayList; import java.util.List; public class list { List<syudent> l; public list() { l=new ArrayList<syudent>(); } //添加 public void add(syudent stu) { l.add(stu); } //删除 public void delete(syudent stu) { l.remove(stu); } //查询 public boolean contain(syudent stu) { return l.contains(stu); } //遍历打印 public void name() { for(syudent s:l){ System.out.println(s); } } }最后编写一个测试类,来调用其他类中的方法
package 学生管理类; public class a { public static void main(String[] args) { list man=new list(); //添加学生 man.add(new syudent("lisi", 18, 80)); man.add(new syudent("ss", 19, 80)); man.add(new syudent("yy", 20, 80)); man.add(new syudent("dd", 99, 80)); man.add(new syudent("cc", 18, 80)); //遍历显示 man.name(); //判断是否包含 System.out.println(man.contain(new syudent("cc", 18, 80))); man.delete(new syudent("cc", 18, 80)); man.name(); } }```