一共需要创建三个类:
1:Student类------存放程序所需的属性和方法
2:Control类------具体的实现方法
3:Test类------测试类
Student类
import java.util.ArrayList;
public class Student {
String name;
int id;
ArrayList<Subject> list;
public Student() {
}
public Student(String name,int id,ArrayList<Subject> list) {
this.name = name;
this.id = id;
this.list = list;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", list=" + list + "]";
}
}
class Subject {
String name;
int score;
public Subject() {
}
public Subject(String name,int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Subject [name=" + name + ", score=" + score + "]";
}
}
Control类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.swing.text.html.HTMLDocument.Iterator;
public class Control {
// 创建全局的list
LinkedList<Student> list = new LinkedList<Student>();
void initStudent() {
Subject subject1 = new Subject("语文",90);
Subject subject2 = new Subject("数学",80);
Subject subject3 = new Subject("英语",88);
ArrayList<Subject> list1 = new ArrayList<Subject>();
list1.add(subject1);
list1.add(subject2);
list1.add(subject3);
Student student1 = new Student("张三",1,list1);
Subject subject11 = new Subject("语文",66);
Subject subject21 = new Subject("数学",65);
Subject subject31 = new Subject("英语",99);
ArrayList<Subject> list2 = new ArrayList<Subject>();
list2.add(subject11);
list2.add(subject21);
list2.add(subject31);
Student student2 = new Student("李四",2,list2);
Subject subject12 = new Subject("语文",78);
Subject subject22 = new Subject("数学",88);
Subject subject32 = new Subject("英语",89);
ArrayList<Subject> list3 = new ArrayList<Subject>();
list3.add(subject12);
list3.add(subject22);
list3.add(subject32);
Student student3 = new Student("王五",3,list3);
Subject subject13 = new Subject("语文",66);
Subject subject23 = new Subject("数学",88);
Subject subject33 = new Subject("英语",56);
ArrayList<Subject> list4 = new ArrayList<Subject>();
list4.add(subject13);
list4.add(subject23);
list4.add(subject33);
Student student4 = new Student("赵六",4,list4);
Subject subject14 = new Subject("语文",66);
Subject subject24 = new Subject("数学",66);
Subject subject34 = new Subject("英语",83);
ArrayList<Subject> list5 = new ArrayList<Subject>();
list5.add(subject14);
list5.add(subject24);
list5.add(subject34);
Student student5 = new Student("温九",5,list5);
this.list.add(student1);
this.list.add(student2);
this.list.add(student3);
this.list.add(student4);
this.list.add(student5);
}
//1,获取指定学生的成绩
public void score(String name) {
//遍历list中的学生
for (Student student : this.list) {
if (name.equals(student.name)) {
List<Subject> subList = student.list;
System.out.println(subList);
}
}
}
//2,获取指定学号的成绩
public void idScore(int id) {
for (Student student : this.list) {
if (student.id == id) {
System.out.println(student.list);
}
}
}
//3,根据学生的id修改姓名
public void idChangeName(int id,String name) {
for (Student student : this.list) {
if (student.id == id) {
System.out.println("修改前的学生的姓名是:" + student.name);
student.name = name;
System.out.println("修改后的学生的姓名是:" + student.name);
}
}
}
//4,根据姓名修改指定学科的成绩
public void changeScore(String name,String sub,int score) {
for (Student student : this.list) {
if (student.name.equals(name)) {
System.out.println("修改前学生的成绩是:" + student.list);
for (Subject subject : student.list) {
if (subject.name.equals(sub)) {
subject.score = score;
System.out.println("修改后的学生的成绩是:" + student.list);
}
}
}
}
}
//5,删除指定学生的成绩
public void delete(String name) {
for (Student student : this.list) {
if (student.name.equals(name)) {
this.list.remove();
System.out.println("删除成功");
for (Student student1 : this.list) {
System.out.println(student1);
}
return;
}
}
}
//6,获取到所有学生的成绩,映射到新的数组中,并进行排序
public void getScore() {
ArrayList<Integer> list1 = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
list1.add(stu.list.get(0).score);
}
System.out.println(list1);
for (int i = 0; i < list1.size() - 1; i++) {
for (int j = 0; j < list1.size()- i - 1; j++) {
if (list1.get(j) > list1.get(j+1)) {
int temp = list1.get(j);
list1.set(j, list1.get(j+1));
list1.set(j+1, temp);
}
}
}
System.out.println(list1);
}
//7*统计一下相同成绩的人的人数
public void number() {
//创建一个HashMap来存放数组的信息,HashMap的键为元素,值为该元素出现的次数
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
//从装学生的list中取出学生信息
for (Student student : list) {
//从装学科的list中取出各科成绩
for (Subject subject : student.list) {
//System.out.println(subject);
if (subject.name == "语文") {
Integer num = map.get(subject.score);
map.put(subject.score, num == null ? 1 : num + 1);
}
}
}
Set keySet = map.keySet(); //用集合接收获取所有的键
for (Object object : keySet) {
System.out.print("成绩:" + object);
System.out.print(",");
System.out.println("出现的次数: " + map.get(object));
}
}
}
Test类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//先创建一个对象
Control control = new Control();
control.initStudent();
//1,
System.out.println("请输入学生的姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.next();
control.score(name);
//2,
System.out.println("请输入学生的id:");
int id = sc.nextInt();
control.idScore(id);
//3,
System.out.println("请输入学生的id:");
int id1 = sc.nextInt();
System.out.println("请输入要修改的姓名:");
String name1 = sc.next();
control.idChangeName(id1, name1);
//4,
control.changeScore("张三", "语文", 100);
//5
control.delete("张三");
//6,
control.getScore();
//7,
control.number();
}
}