我正在尝试为上课的旅行推销员问题生成房屋。如何为我生成的每个房屋指定显式字符串?

时间:2019-05-07 15:32:36

标签: java traveling-salesman

为生成房屋名称,一个街区的一侧有10座房屋,总共20座。街区一侧的房屋以字母A-J表示,街区另一侧的房屋以相同的字母表示,但带有2个相同的字母(例如AA,BB,...)。

    House[][] houses = new House[1000][1000];
    for (int i = 0; i < houses.length; i++) {
        for (int j = 0; j < houses.length; j++) {
            House h = new House(i, j, ""); //Need some way to designate 
    letters for each house 
        }
    }
    //House class
public class House {
    private int streetnum;
    private int avnum;
    private String houseletter = "";

    public House(int streetnum, int avnum, String houseletter) {
        streetnum = this.streetnum;
        avnum = this.avnum;
        houseletter = this.houseletter;
    }
    public int getStreetNum() {
        return streetnum;
    }
    public int getAvNum() {
        return avnum;
    }
    public String getHL() {
        return houseletter;
    }
}

1 个答案:

答案 0 :(得分:0)

不确定这是否回答了您的问题,但是在House类的构造函数中,应该以相反的方式编写赋值。 this指正在创建的House的当前实例。

public House(int streetnum, int avnum, String houseletter) {
    this.streetnum = streetnum;
    this.avnum = avnum;
    this.houseletter = houseletter;
}