个人主页:个人主页
系列专栏:JAVASE基础
看完本节内容后,推荐去刷题巩固基础点击跳转刷题网站进行注册学习
目录
案例1.遍历并删除元素值
案例2.影片信息在程序中的表示
案例3.学生信息系统的数据搜索
public static void main(String[] args) {
int[] arr = {98,77,66,89,79,50,100};
ArrayList 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);
}
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 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("------------------------------------");
}
}
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 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 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;
}
}