【发布时间】:2013-03-14 22:14:31
【问题描述】:
问题是我无法创建唯一的学生编号,有时我在存储学生时会得到相同的学生 ID,是否有生成唯一的学生 ID 编号,我需要修改存储方法以便创建唯一的
public class Collection
{
private ArrayList<Student> studentList;
public Collection()
{
studentList = new ArrayList<Student>();
}
public void storeStudent(Student student)
{
student.setId(createId("AB",9));
studentList.add(student);
}
public String createId(String pre, int number)
{
Random random = new Random();
int index = random.nextInt(number);
return pre + index + " ";
}
}
public class Student
{
private String studentId;
private String name;
public Student( String name)
{
studentId = "UnKnow";
this.name = name;
}
public void setId(String id)
{
studentId = id;
}
}
【问题讨论】:
-
增加一个与数据一起保存的计数器怎么样?
-
Ghassar,如果你继续重复stackoverflow.com/questions/15599427/… 之类的问题并试图让用户在这里做你的工作,这是不可以接受的! -1.
标签: java arrays methods random bluej