Java无法从HashMap调用方法

时间:2017-11-07 23:24:52

标签: java for-loop methods hashmap

我正在尝试增加Bird类中的观察字段。我尝试了几种不同的方法,但我似乎无法让它增加。基本上,我想调用addObservations()在已经在Map中找到鸟的情况下向计数添加一个观察。问题显然在于BirdData中的observe()。以下是代码

    public class Bird {
        private String name;
        private String latinName;
        private int year;
        private int observations;


    public Bird (String name, String latinName, int year) {
        this.name = name;
        this.latinName = latinName;
        this.year = year;
        this.observations = 0;

    }

    public int getObservations() {
        return this.observations;
    }

    public void addObservation() {
        observations++;
    }

    public String getName() {
        return this.name;
    }

    public String getLatinName() {
            return this.latinName;
    }

    public int getYear() {
        return this.year;
    }


    @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        } if (this.getClass() != o.getClass()) {
            return false;
        } 

        Bird compared = (Bird) o;
        if (this.latinName == null || !this.latinName.equals(compared.latinName)){
         return false;   
        } if (this.year != compared.year) {
            return false;
        }



        return true;

        }

        @Override
        public int hashCode() {
            if (this.latinName == null) {
                return 11;
            }


            return this.year +this.latinName.hashCode();


        }

        @Override
        public String toString() {
            return this.name + ", " +this.latinName + ": " + this.year + " " +this.observations;
        }

        }

public class BirdData {


    private Map<Bird, String> birdList;
    private Bird bird;


    public BirdData() {
        this.birdList = new HashMap<Bird, String>();
}


    public void observe (Bird bird, String place) {

        for (Bird b : birdList.keySet()) {
            if (birdList.get(b).equals(bird)){
                b.addObservation();
            }

        } if (!birdList.containsKey(bird)){
                birdList.put(bird, place);

                }

}

    public void observations (Bird bird) {

        if (birdList.containsKey(bird)) {
            System.out.println(bird + " observations: " + bird.getObservations());
        }

    }
}

public class Main {

    public static void main (String[] args) {

        BirdData bl = new BirdData();



        bl.observe(new Bird ("Rose Starling", "Sturnus roseus", 2012), "Arabia");
        bl.observe(new Bird ("Rose Starling", "Sturnus roseus", 2012), "Arabia");
        bl.observe(new Bird ("Rose Starling", "Sturnus roseus", 2012), "Arabia");
        bl.observe(new Bird ("Rose Starling", "Sturnus roseus", 2018), "Arabia");

        bl.observations(new Bird ("Rose Starling", "Sturnus roseus", 2012));
        bl.observations(new Bird ("Rose Starling", "Sturnus roseus", 2018));







    }


}

4 个答案:

答案 0 :(得分:0)

假设错误位于BirdData.observe()。特别是它是一个错误的条件:

birdList.get(b)将返回String,并与bird Bird进行比较。因此,b.addObservation()永远不会被执行。

由于我并不完全熟悉程序的预期逻辑,所以我只能给出如何解决这个问题的提示。请注意仔细选择,以下哪项符合您的要求:

public void observe (Bird bird, String place) {
    bird.addObservation();
}

这忽略了place参数,但是其他一些评论已经指出,place信息本身并没有被过多地使用。也许它不打算在这里使用。

替换

if (birdList.get(b).equals(bird)){

if (bird.equals(b) && birdList.containsKey(b) && birdList.get(b).equals(place)){

(如果您将birdList.containsKey()循环放在for条件中,则可以明确保存else。{/ em} >

这同时考虑了ifbird

答案 1 :(得分:0)

替换

if (birdList.get(b).equals(bird)){

if (birdList.get(b).equals(place)){

基本上,只有在地图中找到所提供的参数 - 鸟和地点作为键值对时,才应将鸟添加到观察列表中。

顺便说一下,你可以用更快(更少代码)的方式来做到这一点:

if(place.equals(birdList.get(bird)) {
     b.addObservation();
}
else {
     birdList.put(bird, place);
}

答案 2 :(得分:0)

目前,您只能使用HashMap<Bird, String>将鸟类目击限制在一个位置。我认为通过将位置保存在鸟类对象内的HashSet<String>中,允许为每只鸟存储多个位置会更有用。

如果您想在此时使用HashMap搜索Bird个对象,它会有点棘手,但您可以将Bird对象用作HashMap中的键和值。< / p>

所以在Bird对象中添加:

HashSet<String> locations;

然后观察你可以做这样的事情:

Map<Bird, Bird> birdList = new HashMap<>();

public void observe (Bird bird, String place) {

    Bird retreivedBird = birdList.get(bird);

    if(retreivedBird != null){
        if(!retreivedBird.locations.contains(place)){
            retreivedBird.locations.add(place);
        }
    }else{
        retreivedBird = bird;
        bird.locations.add(place);
        birdList.put(bird, bird);
    }
    retreivedBird.addObservation();
}

答案 3 :(得分:0)

我感谢所有的帮助。推荐的解决方案都没有根据需要实际增加观察区域。我最终重建了BirdData部分,创建了一个包含Bird和List的HashMap。然后为了保持观察的计数,我使用了birdList.size();.这从Bird完全解决了这个问题。感谢您的反馈。