案例1.遍历并删除元素值
需求:
某个班级的考试在系统上进行,成绩大致为:98, 77, 66, 89, 79, 50, 100
现在需要先把成绩低于80分以下的数据去掉。
分析:
① 定义 ArrayList 集合存储多名学员的成绩。
② 遍历集合每个元素,如果元素值低于 80 分,去掉它。
public static void main(String[] args) { int[] arr = {98,77,66,89,79,50,100}; ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < arr.length; i++) { list.add(arr[i]); } for (int i = 0; i < list.size(); ) { if (list.get(i)<80){ list.remove(i); } else { i++; } } System.out.println(list); }
案例2.影片信息在程序中的表示
需求
某影院系统需要在后台存储上述三部电影,然后依次展示出来。
分析
① :定义一个电影类,定义一个集合存储电影对象。
② :创建3个电影对象,封装相关数据,把3个对象存入到集合中去。
③ :遍历集合中的3个对象,输出相关信息。
电影类:
public class Movie { private String name; private Double score; private String actor; public Movie(String name, Double score, String actor) { this.name = name; this.score = score; this.actor = actor; } public Movie() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getScore() { return score; } public void setScore(Double score) { this.score = score; } public String getActor() { return actor; } public void setActor(String actor) { this.actor = actor; } }
public static void main(String[] args) { Movie m1 = new Movie("电影1",9.1,"演员1"); Movie m2 = new Movie("电影2",8.4,"演员2"); Movie m3 = new Movie("电影3",8.1,"演员3"); ArrayList<Movie> movies = new ArrayList<>(); movies.add(m1); movies.add(m2); movies.add(m3); for (int i = 0; i < movies.size(); i++) { Movie m = movies.get(i); System.out.println("电影名称:"+m.getName()); System.out.println("电影评分:"+m.getScore()); System.out.println("电影演员:"+m.getActor()); System.out.println("------------------------------------"); } }
案例3.学生信息系统的数据搜索
需求
后台程序需要存储如上学生信息并展示,然后要提供按照学号搜索学生信息的功能。
分析
① 定义Student类,定义ArrayList集合存储如上学生对象信息,并遍历展示出来。
② 提供一个方法,可以接收ArrayList集合,和要搜索的学号,返回搜索到的学生对象信息,并展示。
③ 使用死循环,让用户可以不停的搜索。
学生类:
public class Student { private String studentId; private String name; private int age; private String className; public Student(String studentId, String name, int age, String className) { this.studentId = studentId; this.name = name; this.age = age; this.className = className; } public Student() { } public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } }
测试类:
public class base { public static void main(String[] args) { ArrayList<Student> students = new ArrayList<>(); students.add(new Student("2020212201","小王",22,"计算机科学与技术1班")); students.add(new Student("2020213201","小刘",20,"软件工程1班")); students.add(new Student("2020212511","小李",21,"土木工程1班")); students.add(new Student("2020217251","小李",21,"电子商务1班")); System.out.println("学号"+"\t\t\t"+"姓名"+"\t"+"年龄"+"\t"+"班级"); for (int i = 0; i < students.size(); i++) { Student s=students.get(i); System.out.println(s.getStudentId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getClassName()); } Scanner scanner = new Scanner(System.in); Student student; while (true){ System.out.println("请输入要查询的学号"); String id = scanner.next(); student = getStudentById(students, id); if (student==null){ System.out.println("查无此人"); } else { System.out.println(student.getStudentId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getClassName()); } } } public static Student getStudentById(ArrayList<Student> students,String id) { for (int i = 0; i < students.size(); i++) { Student s = students.get(i); if (s.getStudentId().equals(id)){ return s; } } return null; } }