创建学生类Student,包含学号,姓名,性别和年级。 a、使用ArrayList,添加5名学生,遍历展示学生信息。 b、使用Map,添加5名学生。 key是学号,value是学生对象。 c、(选做)使用HashSet存储5名学生,实现去重。学号相同则认为是同一个学生。 提示:重写equeals和hashcode
时间: 2024-03-16 18:42:58 浏览: 50
一个 Student类:包括姓名、学号、性别、专业、家庭地址。
好的,我来回答你的问题。
首先,我明白你要求我不透露你的任何要求,我会尽力回答你的问题并保护你的隐私。
关于你的问题,我来给你详细的解答:
a、创建一个名为Student的类,包含学号,姓名,性别和年级四个属性。然后创建一个ArrayList,将五名学生添加到列表中。最后,使用循环遍历该列表并展示每个学生的信息。
代码示例:
```java
import java.util.ArrayList;
public class Student {
private String id;
private String name;
private String gender;
private int grade;
public Student(String id, String name, String gender, int grade) {
this.id = id;
this.name = name;
this.gender = gender;
this.grade = grade;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("001", "张三", "男", 1));
students.add(new Student("002", "李四", "女", 2));
students.add(new Student("003", "王五", "男", 3));
students.add(new Student("004", "赵六", "女", 4));
students.add(new Student("005", "钱七", "男", 5));
for (Student student : students) {
System.out.println("学号:" + student.getId() + ",姓名:" + student.getName() + ",性别:" + student.getGender() + ",年级:" + student.getGrade());
}
}
}
```
b、创建一个名为Student的类,包含学号,姓名,性别和年级四个属性。然后创建一个Map,将五名学生添加到该Map中,其中key是学号,value是学生对象。最后,使用循环遍历该Map并展示每个学生的信息。
代码示例:
```java
import java.util.HashMap;
import java.util.Map;
public class Student {
private String id;
private String name;
private String gender;
private int grade;
public Student(String id, String name, String gender, int grade) {
this.id = id;
this.name = name;
this.gender = gender;
this.grade = grade;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
public class Main {
public static void main(String[] args) {
Map<String, Student> studentMap = new HashMap<>();
studentMap.put("001", new Student("001", "张三", "男", 1));
studentMap.put("002", new Student("002", "李四", "女", 2));
studentMap.put("003", new Student("003", "王五", "男", 3));
studentMap.put("004", new Student("004", "赵六", "女", 4));
studentMap.put("005", new Student("005", "钱七", "男", 5));
for (Map.Entry<String, Student> entry : studentMap.entrySet()) {
Student student = entry.getValue();
System.out.println("学号:" + student.getId() + ",姓名:" + student.getName() + ",性别:" + student.getGender() + ",年级:" + student.getGrade());
}
}
}
```
c、创建一个名为Student的类,包含学号,姓名,性别和年级四个属性。然后创建一个HashSet,将五名学生添加到该HashSet中,实现去重,即学号相同则认为是同一个学生。需要在Student类中重写equals和hashCode方法。
代码示例:
```java
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class Student {
private String id;
private String name;
private String gender;
private int grade;
public Student(String id, String name, String gender, int grade) {
this.id = id;
this.name = name;
this.gender = gender;
this.grade = grade;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@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);
}
}
public class Main {
public static void main(String[] args) {
Set<Student> studentSet = new HashSet<>();
studentSet.add(new Student("001", "张三", "男", 1));
studentSet.add(new Student("002", "李四", "女", 2));
studentSet.add(new Student("003", "王五", "男", 3));
studentSet.add(new Student("004", "赵六", "女", 4));
studentSet.add(new Student("001", "钱七", "男", 5));
for (Student student : studentSet) {
System.out.println("学号:" + student.getId() + ",姓名:" + student.getName() + ",性别:" + student.getGender() + ",年级:" + student.getGrade());
}
}
}
```
以上就是关于创建学生类并使用ArrayList、Map和HashSet存储的详细解答,希望对你有所帮助。
阅读全文