如何限制ArrayList中的对象数量

时间:2015-08-16 20:11:34

标签: java arraylist bidirectional

我正在为我的O.O.P做一些修改。考试即将在下周进行,我会遇到一个问题。问题基本上是一个狗和跳蚤之间的双向关联的例子。到目前为止,我有一只带跳蚤的狗。我坚持的部分是,“修改狗类,以便狗对象最多只能容纳5个跳蚤对象(打印”你的狗有太多的跳蚤!“如果有超过5个跳蚤)。这是我的代码到目前为止:

Dog.java

import java.util.ArrayList;

public class Dog {

    private String name;
    private int age;
    private String address;

    ArrayList<Flea> fleas = new ArrayList<Flea>(); {
        if(fleas.size() > 5) {
             System.out.println("This dog has too many fleas!");
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void hostFlea(Flea flea) {
        fleas.add(flea);
    }

    public ArrayList<Flea> getDogFlea() {
         return fleas;
    }

    public String toString() {
        return name + " the Dog (aged " + age + ") has fleas. \nThey are: " + fleas + ".";
    }   

}

Flea.java

public class Flea {

    private String name;
    private int age; 

    public Flea (String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return name + " (aged " + age + ")";
    }

}

Test.java

public class Test {

    public static void main(String[] args) {

        Dog dog = new Dog();
            dog.setName("Freddy");
            dog.setAddress("Cork");
            dog.setAge(5);

        Flea flea1 = new Flea("John", 1);
        dog.hostFlea(flea1);

        Flea flea2 = new Flea("Patrick", 3);        
        dog.hostFlea(flea2);

        Flea flea3 = new Flea("Alan", 7);
        dog.hostFlea(flea3);

        Flea flea4 = new Flea("Steven", 2);
        dog.hostFlea(flea4);

        Flea flea5 = new Flea("Charles", 5);
        dog.hostFlea(flea5);

        Flea flea6 = new Flea("Derek", 1);
        dog.hostFlea(flea6);

        Flea flea7 = new Flea("Kevin", 8);
        dog.hostFlea(flea7);

        System.out.println(dog);

    }

}

控制台:

弗雷迪狗(5岁)有跳蚤。 他们是:[John(1岁),Patrick(3岁),Alan(7岁),Steven(2岁),Charles(5岁),Derek(1岁),Kevin(8岁)。

4 个答案:

答案 0 :(得分:1)

在此处添加检查您的情况:

public void hostFlea(Flea flea) {
    if(fleas.size() >= 5) {
        System.out.println("This dog has too many fleas!");
    } else {
        fleas.add(flea);
    }
}

不是你的列表变量的定义(正如你所做的那样),因为你刚刚添加了an instance initialization block

答案 1 :(得分:1)

我猜你被迫使用ArrayList。所以你必须确保没有人修改其他作为你的狗类的列表。因此,将列表设为私有

private ArrayList<Flea> fleas = new ArrayList<Flea>();

返回副本,不要在getDogFlea方法

中公开原始列表
public ArrayList<Flea> getDogFlea() {
     return new ArrayList(fleas); //use copy constructor to not expose original list
}

并在hostFlea方法中添加支票。

public void hostFlea(Flea flea) {
    if(fleas.size() == 5 ) {
        System.out.println("This dog has too many fleas!");
        return; //so the 6th element is not added
    }
    fleas.add(flea);
}

对于你的考试而言,也许最后一次是足够的,但是在真实的程序中,getter会出现问题,确保不会有更多的5个元素。

答案 2 :(得分:1)

假设添加跳蚤的公共方法是hostFlea,所以你需要改变它:

public void hostFlea(Flea flea) {
    // If the dog already has at least 5 fleas, you can't add another
    if (fleas.size() >= 5) {
        System.out.println("Your dog has too many fleas!");
    } else {
        fleas.add(flea);
    }
}

但是,getDogFlea()会返回内部ArrayList,因此无法阻止确定的用户调用dog.getDogFlea().add(flea6)。为了防止此类行为,您可以复制数据:

public ArrayList<Flea> getDogFlea() {
     return new ArrayList<>(fleas);
}

或者,在放宽API以返回List而不是ArrayList的“成本”时,教科书解决方案将使用Collections.unmodifiableList

public List<Flea> getDogFlea() {
     return Collections.unmodifiableList(fleas);
}

答案 3 :(得分:0)

在你的hostFlea方法中: -

在将Flea对象添加到Dog类中的跳蚤ArrayList之前,只需检查此arraylist的大小。

如: -

public void hostFlea(Flea flea){
if(fleas.size() > 5) {
  System.out.println("This dog has too many fleas!");
        }
 else{
fleas.add(flea);
   }
}