来做计ç®挑æ题了
This commit is contained in:
@@ -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
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/nuccu.iml" filepath="$PROJECT_DIR$/nuccu.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -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<Student>集合,遍历集合,输出集合中各个学生的信息。
|
||||||
|
* 输入说明:输入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<Student>[] 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user