麻烦老师帮忙看看,用的集合不对么?
package com.imooc.lx8;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.print.attribute.HashAttributeSet;
public class StudentTest {
public static void main(String[] args) {
//定义三个Student类的对象及一个HashSet类的对象
Student one = new Student(3,"William",65);
Student to = new Student(1,"Tom",87);
Student su = new Student(2,"Lucy",95);
//将Student类的对象添加到集合中
ArrayList li = new ArrayList();
li.add(one);
li.add(to);
li.add(su);
//使用迭代器显示Student类的对象中的内容
Iterator it = li.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
package com.imooc.lx8;
public class Student {
private int stuld;
private String name;
private float score;
public Student(int stuld, String name, float score) {
super();
this.stuld = stuld;
this.name = name;
this.score = score;
}
public int getStuld() {
return stuld;
}
public void setStuld(int stuld) {
this.stuld = stuld;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "[学号:" + stuld + ", 姓名:" + name + ", 成绩:" + score + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + Float.floatToIntBits(score);
result = prime * result + stuld;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Float.floatToIntBits(score) != Float.floatToIntBits(other.score))
return false;
if (stuld != other.stuld)
return false;
return true;
}
}
正在回答
同学的代码没有问题,问一下同学在线编程中测试时,是否写了包名呢。建议同学去掉包名再试试。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星