当存储给学生时可能碰巧有相同的 id,所以我怎样才能避免重复,我尝试了 while 循环和 for each 循环,但它没有用
public void addStudent(Student student)
{
student.setId(ganerateNewRandom("aa", 2));
students.add(student);
}
public String ganerateNewRandom(String prefix, int number)
{
Random random = new Random();
StringBuffer ret = new StringBuffer(prefix);
for(int i=0;i<number;i++) ret.append(random.nextInt(10));
return ret.toString();
}