集合存储自定义并遍历,通过学号搜索学生信息
【摘要】
package ArrayList;
//定义学生类
public class Student {
String id;
String name;
String sex;
...
package ArrayList;
//定义学生类
public class Student {
String id;
String name;
String sex;
String age;
String tel;
public Student() {
}
public Student(String id, String name, String sex, String age, String tel) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.tel = tel;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
package ArrayList;
import java.util.ArrayList;
import java.util.Scanner;
//集合存储自定义并遍历,通过学号搜索学生信息
public class ArrayListTest3 {
public static void main(String[] args) {
//创建学生对象
Student s1=new Student("001","小马哥","男","44","11111111111");
Student s2=new Student("002","小飞侠","男","47","22222222222");
Student s3=new Student("003","马奎斯","男","44","33333333333");
//创建ArrayList集合,并存储学生对象
ArrayList<Student> list=new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
//遍历集合
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getSex()+"\t"+student.getAge()+"\t"+student.getTel());
}
//调用getStudentById方法
while (true){
Scanner sc=new Scanner(System.in);
System.out.println("输入你要查询学生的id:");
String id = sc.next();
Student student = getStudentById(list, id);
if (student==null){
System.out.println("没有此学生!");
}else {
System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getSex()+"\t"+student.getAge()+"\t"+student.getTel());
break;
}
}
}
//通过学号搜索学生信息
public static Student getStudentById(ArrayList<Student> list, String id){
//遍历全部学生对象
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
//判断查找的学号是否在集合中
if (student.getId().equals(id)){
//返回学生对象
return student;
}
}
//没有查到
return null;
}
}
/*
001 小马哥 男 44 11111111111
002 小飞侠 男 47 22222222222
003 马奎斯 男 44 33333333333
输入你要查询学生的id:
003
003 马奎斯 男 44 33333333333
*/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
文章来源: blog.csdn.net,作者:不会压弯的小飞侠,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43514330/article/details/125085032
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)