From e6b23db7ff1359d667da157ad5abc16beabf103c Mon Sep 17 00:00:00 2001 From: msksbr Date: Thu, 26 Sep 2024 21:14:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=A5=E5=81=9A=E8=AE=A1=C3=A7=C2=AE?= =?UTF-8?q?=E6=8C=91=C3=A6=C2=88=E9=A2=98=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/nuccu/.gitignore | 29 ++++++++++++++++ test/nuccu/.idea/.gitignore | 8 +++++ test/nuccu/.idea/misc.xml | 6 ++++ test/nuccu/.idea/modules.xml | 8 +++++ test/nuccu/.idea/vcs.xml | 6 ++++ test/nuccu/nuccu.iml | 11 ++++++ test/nuccu/src/y22/q16/Main.java | 47 ++++++++++++++++++++++++++ test/nuccu/src/y22/q16/Student.java | 52 +++++++++++++++++++++++++++++ 8 files changed, 167 insertions(+) create mode 100644 test/nuccu/.gitignore create mode 100644 test/nuccu/.idea/.gitignore create mode 100644 test/nuccu/.idea/misc.xml create mode 100644 test/nuccu/.idea/modules.xml create mode 100644 test/nuccu/.idea/vcs.xml create mode 100644 test/nuccu/nuccu.iml create mode 100644 test/nuccu/src/y22/q16/Main.java create mode 100644 test/nuccu/src/y22/q16/Student.java diff --git a/test/nuccu/.gitignore b/test/nuccu/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/test/nuccu/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/test/nuccu/.idea/.gitignore b/test/nuccu/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/test/nuccu/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/test/nuccu/.idea/misc.xml b/test/nuccu/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/test/nuccu/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/test/nuccu/.idea/modules.xml b/test/nuccu/.idea/modules.xml new file mode 100644 index 0000000..a8721c9 --- /dev/null +++ b/test/nuccu/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/test/nuccu/.idea/vcs.xml b/test/nuccu/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/test/nuccu/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/test/nuccu/nuccu.iml b/test/nuccu/nuccu.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/test/nuccu/nuccu.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/test/nuccu/src/y22/q16/Main.java b/test/nuccu/src/y22/q16/Main.java new file mode 100644 index 0000000..6bf4396 --- /dev/null +++ b/test/nuccu/src/y22/q16/Main.java @@ -0,0 +1,47 @@ +package y22.q16; + +/* HashSet是Java中常用的Set集合,向HashSet集合中添加数据对象时,首先会调用对象的hashCode()方法获取哈希码,根据哈希码计算对象的存储位置,如果相应位置上已经有数据对象,则会调用对象的equals()方法判断新加入的对象与现有对象是否重复,如果重复则拒绝加入。为了使用HashSet集合正确存储自定义类的对象,自定义类需要重写equals()方法和hashCode()方法。 +* 编写程序完成以下功能:定义一个学生类Student,属性包括String类型的id、name和int类型的age(id属性字符串内容相同的则认为是同一个学生),为三个属性定义get和set方法。重写equals()方法和hashCode()方法,使得当使用HashSet存储Student类的对象时,id重复的学生对象不能重复添加到集合。输入4个学生类对象的属性值,创建4个Student类对象,将其添加到一个HashSet集合,遍历集合,输出集合中各个学生的信息。 +* 输入说明:输入4个Student类对象的属性值,每个对象属性值按一行输入,以空格分割。 +* 输出说明:集合中存储的Student类对象的信息,每个对象信息占一行,输出格式为 id:name:age。 +* 输入样例1: +* 2022001 Tom 19 +* 2022002 Jerry 18 +* 2022003 Eason 20 +* 2022002 Jerry 18 +* 输入样例2: +* 2022001 Tom 19 +* 2022002 Jerry 18 +* 2022003 Eason 20 +* 2022004 Jerry 18 + +* 输出样例1: +* 2022001:Tom:19岁 +* 2022002:Jerry:18岁 +* 2022003:Eason:20岁 +* 输出样例2: +* 2022001:Tom:19岁 +* 2022002:Jerry:18岁 +* 2022003:Eason:20岁 +* 2022004:Jerry:18岁*/ + +import java.util.HashSet; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Student[] students = new Student[4]; + HashSet[] StudentSet; + for (int i = 0; i < students.length; i++) { + students[i] = new Student(); + students[i].setId(sc.next()); + students[i].setName(sc.next()); + students[i].setAge(sc.nextInt()); + } + StudentSet = students; + for (int i = 0; i < StudentSet.length; i++) { + System.out.println(StudentSet[i].toString()); + } + } +} diff --git a/test/nuccu/src/y22/q16/Student.java b/test/nuccu/src/y22/q16/Student.java new file mode 100644 index 0000000..8cca342 --- /dev/null +++ b/test/nuccu/src/y22/q16/Student.java @@ -0,0 +1,52 @@ +package y22.q16; + +import java.util.HashSet; +import java.util.Objects; + +public class Student extends HashSet { + private String id; + private String name; + private int age; + + 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 getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if(o==null||getClass()!=o.getClass()) return false; + Student student = (Student) o; + return Objects.equals(id,student.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return this.id+":"+this.name+":"+this.age; + } +}