1.对象数组概述
A:基本类型的数组:存储的元素为基本类型
int[] arr={1,2,3,4}
B:对象数组:存储的元素为引用类型
Student[] stus=new Student[3];
Student代表一个自定义类
Stus数组中stus[0],stus[1],stus[2]的元素数据类型为Student, 都可以指向一个Student对象
2.ArrayList删改查方法
A:获取元素
public E get(int index):返回指定索引处的元素
B:集合长度
public int size():返回集合中的元素的个数
C:删除元素
public boolean remove(Object o):删除指定的元素,返回删除是否成功
public E remove(int index):删除指定索引处的元素,返回被删除的元素
D:修改元素
public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
学生管理系统
package StudentMange;
public class Student {
private String id;
private String name;
private int score;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String id, String name, int score) {
super();
this.id = id;
this.name = name;
this.score = score;
}
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 int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
package StudentMange;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentMange {
private static Scanner s = new Scanner(System.in);
/*
* 定义这个集合为成员变量是为了让说有的方法可以对之进行使用。
*/
private static List<Student> list = new ArrayList<>();
public static void main(String[] args) {
printWelcom();
/*
* Scanner s = new Scanner(System.in); 因为要多次使用所以将之定义为成员变量
*/
while (true) {
System.out.println("请输入你的选择");
String info = s.nextLine();
switch (Integer.parseInt(info)) {
case 1: {
lookAllStudent();
break;
}
case 2: {
addStudent();
break;
}
case 3: {
delStudent();
break;
}
case 4: {
setStudent();
break;
}
case 5: {
exit();
break;
}
}
}
}
/**
* 查看所有学生
*/
public static void lookAllStudent() {
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
System.out.println("编号:" + s.getId() + " 姓名:" + s.getName() + " 成绩:" + s.getScore());
}
}
/**
* 添加学生
*/
public static void addStudent() {
System.out.println("请输入学生编号:");
String id = s.nextLine();
// 判断id是否已经存在
for (int i = 0; i < list.size(); i++) {
Student addstu = list.get(i);
// 重新输入
if (id.equals(addstu.getId())) {
addStudent();
return;
}
}
System.out.println("请输入学生姓名:");
String name = s.nextLine();
System.out.println("请输入学生成绩:");
String score = s.nextLine();
Student addstu = new Student(id, name, Integer.parseInt(score));
System.out.println("添加成功!");
list.add(addstu);
}
/**
* 删除学生
*/
public static void delStudent() {
System.out.println("请要删除的编号:");
String id = s.nextLine();
int index = 0;
for(int i=0;i<list.size();i++) {
Student s = list.get(i);
if(id.equals(s.getId())) {
index = i;
}
}
list.remove(index);
}
/**
* 修改学生
*/
public static void setStudent() {
System.out.println("请输入学生编号:");
String id = s.nextLine();
Student foundstu = null;
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
if (id.equals(s.getId())) {
foundstu = s;
break;
}
}
// 学生没有找到
if (foundstu == null) {
System.out.println("你输入的编号不存在");
setStudent();
return;
}
System.out.println("请输入学生姓名:");
String name = s.nextLine();
System.out.println("请输入学生成绩:");
String score = s.nextLine();
// 将修改的数据传给学生
foundstu.setName(name);
foundstu.setScore(Integer.parseInt(score));
System.out.println("修改成功!");
}
/**
* 退出
*/
public static void exit() {
System.out.println("谢谢使用学生管理系统,欢迎下次使用!");
System.exit(0);
}
/**
* 欢迎界面
*/
public static void printWelcom() {
System.out.println("-----欢迎来到学生管理系统-------");
System.out.println("1 查看所有学生");
System.out.println("2 添加学生");
System.out.println("3 删除学生");
System.out.println("4 修改学生");
System.out.println("5 退出");
}
}
}