一共需要?jiǎng)?chuàng)建三個(gè)類:
1:Student類------存放程序所需的屬性和方法
2:Control類------具體的實(shí)現(xiàn)方法
3:Test類------測(cè)試類
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 {
// 創(chuàng)建全局的list
LinkedList<Student> list = new LinkedList<Student>();
void initStudent() {
Subject subject1 = new Subject("語(yǔ)文",90);
Subject subject2 = new Subject("數(shù)學(xué)",80);
Subject subject3 = new Subject("英語(yǔ)",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("語(yǔ)文",66);
Subject subject21 = new Subject("數(shù)學(xué)",65);
Subject subject31 = new Subject("英語(yǔ)",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("語(yǔ)文",78);
Subject subject22 = new Subject("數(shù)學(xué)",88);
Subject subject32 = new Subject("英語(yǔ)",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("語(yǔ)文",66);
Subject subject23 = new Subject("數(shù)學(xué)",88);
Subject subject33 = new Subject("英語(yǔ)",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("語(yǔ)文",66);
Subject subject24 = new Subject("數(shù)學(xué)",66);
Subject subject34 = new Subject("英語(yǔ)",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,獲取指定學(xué)生的成績(jī)
public void score(String name) {
//遍歷list中的學(xué)生
for (Student student : this.list) {
if (name.equals(student.name)) {
List<Subject> subList = student.list;
System.out.println(subList);
}
}
}
//2,獲取指定學(xué)號(hào)的成績(jī)
public void idScore(int id) {
for (Student student : this.list) {
if (student.id == id) {
System.out.println(student.list);
}
}
}
//3,根據(jù)學(xué)生的id修改姓名
public void idChangeName(int id,String name) {
for (Student student : this.list) {
if (student.id == id) {
System.out.println("修改前的學(xué)生的姓名是:" + student.name);
student.name = name;
System.out.println("修改后的學(xué)生的姓名是:" + student.name);
}
}
}
//4,根據(jù)姓名修改指定學(xué)科的成績(jī)
public void changeScore(String name,String sub,int score) {
for (Student student : this.list) {
if (student.name.equals(name)) {
System.out.println("修改前學(xué)生的成績(jī)是:" + student.list);
for (Subject subject : student.list) {
if (subject.name.equals(sub)) {
subject.score = score;
System.out.println("修改后的學(xué)生的成績(jī)是:" + student.list);
}
}
}
}
}
//5,刪除指定學(xué)生的成績(jī)
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,獲取到所有學(xué)生的成績(jī),映射到新的數(shù)組中,并進(jìn)行排序
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*統(tǒng)計(jì)一下相同成績(jī)的人的人數(shù)
public void number() {
//創(chuàng)建一個(gè)HashMap來存放數(shù)組的信息,HashMap的鍵為元素,值為該元素出現(xiàn)的次數(shù)
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
//從裝學(xué)生的list中取出學(xué)生信息
for (Student student : list) {
//從裝學(xué)科的list中取出各科成績(jī)
for (Subject subject : student.list) {
//System.out.println(subject);
if (subject.name == "語(yǔ)文") {
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("成績(jī):" + object);
System.out.print(",");
System.out.println("出現(xiàn)的次數(shù): " + map.get(object));
}
}
}
Test類
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//先創(chuàng)建一個(gè)對(duì)象
Control control = new Control();
control.initStudent();
//1,
System.out.println("請(qǐng)輸入學(xué)生的姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.next();
control.score(name);
//2,
System.out.println("請(qǐng)輸入學(xué)生的id:");
int id = sc.nextInt();
control.idScore(id);
//3,
System.out.println("請(qǐng)輸入學(xué)生的id:");
int id1 = sc.nextInt();
System.out.println("請(qǐng)輸入要修改的姓名:");
String name1 = sc.next();
control.idChangeName(id1, name1);
//4,
control.changeScore("張三", "語(yǔ)文", 100);
//5
control.delete("張三");
//6,
control.getScore();
//7,
control.number();
}
}