Studentz类
package com.jk.p235;
import java.util.Objects;
public class Studentz {
String id;//学号
String name;//姓名
int score;//成绩
public Studentz() {
}
public Studentz(String id, String name, int score) {
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 double getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "id=" + id +
", name='" + name + ''' +
", score=" + score +"n";
}
public void show(){
System.out.println(id+"t"+name+"tt"+score);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Studentz)) return false;
Studentz studentz = (Studentz) o;
return id.equals(studentz.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
StudentTest类
package com.jk.p235;
import java.security.KeyStore;
import java.util.*;
public class TestStudentz {
public static void main(String[] args) {
Studentz stu1=new Studentz("1001","张三",80);
Studentz stu2=new Studentz("1002","李四",90);
Studentz stu3=new Studentz("1003","王五",66);
Studentz stu4=new Studentz("1004","王岩",88);
Studentz stu5=new Studentz("1005","啊扣",99);
Set set1=new HashSet<>();
set1.add(stu1);
set1.add(stu2);
set1.add(stu3);
set1.add(stu4);
set1.add(stu5);
for(Studentz G:set1){
System.out.println(G.toString());
}
Map map1= new HashMap<>();
map1.put("1001","张三");
map1.put("1002","李四");
map1.put("1003","王五");
map1.put("1004","王岩");
map1.put("1005","啊扣");
System.out.println("----------通过Map集合-----------");
System.out.println("1001="+map1.get("1001"));
System.out.println("1002="+map1.get("1002"));
System.out.println("1003="+map1.get("1003"));
System.out.println("1004="+map1.get("1004"));
System.out.println("1005="+map1.get("1005"));
System.out.println("map1集合中的键"+map1.keySet());
System.out.println("map1集合中的值"+map1.values());
//打印键集合
System.out.println("----使用 for-each 循环遍历 key 和 values----");
for(String key: map1.keySet()){
System.out.println(key);
}
//打印值集合
for(String value: map1.values()){
System.out.println(value);
}
System.out.println("----在 for 循环中使用 entries 实现 Map 的遍历----");
for(Map.Entry entry: map1.entrySet()){
String mapKey = entry.getKey();
String mapValue = entry.getValue();
System.out.println(mapKey + ":" + mapValue);
}
System.out.println("----使用迭代器(Iterator)遍历----");
Iterator>entries=map1.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = entries.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ":" + value);
}
Studentz[] arr = new Studentz[5];
arr[0] = new Studentz("1001", "张三", 80);
arr[1] = new Studentz("1002", "李四", 90);
arr[2] = new Studentz("1003", "王五", 66);
arr[3] = new Studentz("1004", "王岩", 88);
arr[4] = new Studentz("1005", "啊扣", 99);
sortStudentz(arr);
System.out.println("------------toString输出-----------");//将排序后的结果通过toString方法输出
System.out.println(Arrays.toString(arr));
System.out.println("-----------遍历输出-----------");//将排序后的结果通过遍历输出
System.out.println("idtt" + "namet" + "score");
for (Studentz S : arr) {
S.show();
}
//将按照成绩排序好的数据存入ArrayList集合中
List list = new ArrayList<>(Arrays.asList(arr));
System.out.println("------------ArrayList------------");
for (Studentz F : list) {
System.out.println(F);
}
//将按照成绩排序好的数据存入HashSet集合中
Set set = new HashSet<>(Arrays.asList(arr));
System.out.println("----------HashSet------------");
for (Studentz T : set) {
System.out.println(T);
}
}
//排序方法
public static void sortStudentz(Studentz[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j].getScore() < arr[j + 1].getScore()) {
Studentz temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
此为21年11月18日作业,于次日完成
里面都有注释,不懂得可以私信作者。



