我正在尝试使用toString()
内部对象的方法ArrayList
,它正在打印内存地址而不是预期值。我把我的课程翻译成英文,如果我忘记了什么,抱歉,如果你不明白,请追问。让我们看看我的代码:
安排课程(我抑制了 getter 和 setter):
static Professor professor;
static Room room;
static Classroom classroom;
public String toString() {
return "Schedule [getProfessor()=" + getProfessor() + ", getRoom()="
+ getRoom() + ", getClassRoom()=" + getClassRoom() + ", getClass()=" + getClass() + ", hashCode()="
+ hashCode() + ", toString()=" + super.toString() + "]";
}
学校
ArrayList<Professor> professor = new ArrayList<Professor>();
ArrayList<Room> room = new ArrayList<Room>();
ArrayList<Classroom> classroom = new ArrayList<Classroom>();
public ArrayList<Schedule[][]> sched = new ArrayList<Schedule[][]>();
我正在使用遗传算法(目前我正在学习它,所以我不太了解),这是我的“人口生成器”:
人口
public static void generatePopulation(School school){
// Shuffle the arraylist for generate a random population
Collections.shuffle(school.getProfessor());
Collections.shuffle(school.getClassroom());
Collections.shuffle(school.getRoom());
Schedule[][] sched1 = new Schedule[1][1];
// Generate population (just a little test to see if it works)
sched1[0][0].addEverything(school.getProfessor().get(0), school.getClassroom().get(0), school.getRoom().get(0));
school.sched.add(sched);
主要的
(... school and associations created ...)
Population.generatePopulation(school);
System.out.println(school.getSched().isEmpty()); // it returns false, that is, isn't empty
System.out.println(school.getSched().get(0).toString());
// returns "[[Lentidades.Schedule;@40914272" (entidades = name of my package)
那么,为什么我收到的是内存位置而不是预期值?我想我什么都没有忘记,但是如果您在这段代码中看到一些奇怪的东西,只需发送一个问题,我将解释/粘贴剩余的代码。