我们第一步需要有学生类,用来确定学生都有哪些属性
class Student{
private String name;//姓名
private int age;//年龄
private String no;//学号
public Student() {}
public Student(String name,int age,String no) {
this.name=name;
this.age=age;
this.no=no;
}
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 getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
这里我们写了学生有姓名name年龄age学号no
接下来我们创建用来存储学生对象
ArrayList arrayList=new ArrayList<>();
我们先创建几个学生对象作为默认值来添加到数组里面
Student s1=new Student("李华",15,"12345");
Student s2=new Student("小明",15,"20221514113");
Student s3=new Student("小刚",15,"20221514115");
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
接下来我们需要确定我们需要实现的功能基于集合的函数我这里写了4个功能
1.添加
public static void addStudent(ArrayList arrayList) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入学号:");
String newno=scanner.next();//记录学号
int flag=0;//用来记录学号是否已经存在
for (int i = 0; i < arrayList.size(); i++) {
Student student=arrayList.get(i);//调用get方法来得到集合中的每一个学生对象
if (student.getNo().equals(newno)) {
System.out.println("该学号已经存在");
flag=0;
break;
}
else {
//学号不存在的情况
flag=1;
}
}
if (flag==1) {
System.out.println("请输入姓名:");
String newname=scanner.next();
System.out.println("请输入年龄:");
int newage=scanner.nextInt();
arrayList.add(new Student(newname,newage,newno));//使用匿名对象添加进入数组
System.out.println("添加成功");
}
}
2.删除
public static void delete(ArrayList arrayList) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入删除学生的学号:");
String no=scanner.next();
int index=-1;//用来记录该学号在集合中的位置
for (int i = 0; i < arrayList.size(); i++) {
Student student=arrayList.get(i);
if (no.equals(student.getNo())) {
//如果学号相等
index=i;//吧这个学生所在位置赋值给index
break;
}
}
if (index==-1) {
System.out.println("学号不存在");
}
else {
//调用集合的remove方法来进行删除
arrayList.remove(index);
System.out.println("删除成功");
}
}
3.查询
public static void searchStudent(ArrayList arrayList) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要查询学生的学生号:");
String noString=scanner.next();
int index=-1;//用来记录下标
for (int i = 0; i < arrayList.size(); i++) {
Student s=arrayList.get(i);
if (s.getNo().equals(noString)) {
index=i;
break;
}
}
if (index==-1) {
System.out.println("未查询到该学生信息");
}
else {
Student s=arrayList.get(index);
System.out.println("姓名:"+s.getName()+"年龄:"+s.getAge()+"学号:"+s.getNo());
}
}
4.全部显示
public static void showAll(ArrayList arrayList) {
if (arrayList.size()==0) {
System.out.println("现在还没有学生");
}
else {
//通过遍历来输出全部学生信息
for (int i = 0; i < arrayList.size(); i++) {
Student student=arrayList.get(i);
System.out.println("姓名:"+student.getName()+"年龄:"+student.getAge()+"学号:"+student.getNo());
}
}
}
全部代码
package com.lxy.string;
import java.util.ArrayList;
import java.util.Scanner;
public class demo03 {
public static void main(String[] args) {
ArrayList arrayList=new ArrayList<>();
Student s1=new Student("李华",15,"12345");
Student s2=new Student("小明",15,"20221514113");
Student s3=new Student("小刚",15,"20221514115");
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
while (true) {
System.out.println("-----学生管理系统-----");
System.out.println("(1)添加学生信息");
System.out.println("(2)删除学生信息");
System.out.println("(3)查询学生信息");
System.out.println("(4)全部学生信息");
System.out.println("(5)退出管理系统");
System.out.println("请选择系统功能: ");
Scanner scanner=new Scanner(System.in);
int input=scanner.nextInt();
switch (input) {
case 1:
addStudent(arrayList);
break;
case 2:
delete(arrayList);
break;
case 3:
searchStudent(arrayList);
break;
case 4:
showAll(arrayList);
break;
case 5:
System.exit(0);
break;
default:
break;
}
}
}
public static void addStudent(ArrayList arrayList) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入学号:");
String newno=scanner.next();
int flag=0;
for (int i = 0; i < arrayList.size(); i++) {
Student student=arrayList.get(i);
if (student.getNo().equals(newno)) {
System.out.println("该学号已经存在");
flag=0;
break;
}
else {
flag=1;
}
}
if (flag==1) {
System.out.println("请输入姓名:");
String newname=scanner.next();
System.out.println("请输入年龄:");
int newage=scanner.nextInt();
arrayList.add(new Student(newname,newage,newno));
System.out.println("添加成功");
}
}
public static void delete(ArrayList arrayList) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入删除学生的学号:");
String no=scanner.next();
int index=-1;
for (int i = 0; i < arrayList.size(); i++) {
Student student=arrayList.get(i);
if (no.equals(student.getNo())) {
index=i;
break;
}
}
if (index==-1) {
System.out.println("学号不存在");
}
else {
arrayList.remove(index);
System.out.println("删除成功");
}
}
public static void showAll(ArrayList arrayList) {
if (arrayList.size()==0) {
System.out.println("现在还没有学生");
}
else {
for (int i = 0; i < arrayList.size(); i++) {
Student student=arrayList.get(i);
System.out.println("姓名:"+student.getName()+"年龄:"+student.getAge()+"学号:"+student.getNo());
}
}
}
public static void searchStudent(ArrayList arrayList) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要查询学生的学生号:");
String noString=scanner.next();
int index=-1;
for (int i = 0; i < arrayList.size(); i++) {
Student s=arrayList.get(i);
if (s.getNo().equals(noString)) {
index=i;
break;
}
}
if (index==-1) {
System.out.println("未查询到该学生信息");
}
else {
Student s=arrayList.get(index);
System.out.println("姓名:"+s.getName()+"年龄:"+s.getAge()+"学号:"+s.getNo());
}
}
}
class Student{
private String name;//姓名
private int age;//年龄
private String no;//学号
public Student() {}
public Student(String name,int age,String no) {
this.name=name;
this.age=age;
this.no=no;
}
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 getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
主要锻炼的是对集合方法的考察,以及对集合的遍历。