求佬帮忙,这样做为什么为什么不对
本人思路是先把数据存到数组里,将数组从尾到头再添加到hashset里进行消除重复项,消除完重复项后再将结果放到另一个数组里,再将其反向输出,测试样例是正确的,但提交结果不对
import java.util.*;
import java.math.BigInteger;
import java.math.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Math.abs;
import static java.lang.Math.log;
class Students {
private String name;
private String age;
private String id;
private String sex;
public Students(String id,String name,String age,String sex) {
this.id = id;
this.name = name;
this.age=age;
this.sex=sex;
}
public int hashCode() {
return id.hashCode();
}
public boolean equals(Object obj) {
if(this==obj) return true;
if(!(obj instanceof Students)){
return false;
}
Students stu=(Students) obj;
return this.id.equals(stu.id);
}
public String toString(){
return id+" "+name+" "+age+" "+sex;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
LinkedHashSet set=new LinkedHashSet();
Students[] m=new Students[n];
for(int i=0;i<n;i++){
String id=sc.next();
String name=sc.next();
String age=sc.next();
String sex=sc.next();
m[i]=new Students(id,name,age,sex);
}
for(int i=n-1;i>=0;i--) set.add(m[i]);
System.out.println(set.size());
Students[] m1=new Students[set.size()];
Iterator it=set.iterator();
int t=0;
while(it.hasNext()){
Object obj=(Object) it.next();
m1[t++]=(Students) obj;
}
for(int i=set.size()-1;i>0;i--){
System.out.println(m1[i]);
}
System.out.print(m1[0]);
}
}