代码之家  ›  专栏  ›  技术社区  ›  Jaywin

如何限制ArrayList中的对象数量

  •  3
  • Jaywin  · 技术社区  · 9 年前

    我正在为下周即将到来的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 + ".";
        }   
    
    }
    

    跳蚤.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 + ")";
        }
    
    }
    

    测试.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岁)身上有跳蚤。 他们是:[约翰(1岁),帕特里克(3岁),艾伦(7岁),史蒂文(2岁),查尔斯(5岁),德里克(1岁,凯文(8岁)]。

    4 回复  |  直到 9 年前
        1
  •  1
  •   Community CDub    8 年前

    在此处添加检查条件:

    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 .

        2
  •  1
  •   mszalbach    9 年前

    我猜你是被迫使用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);
    }
    

    也许最后一个对你的考试来说已经足够了,但在一个真正的程序中,吸气者会遇到一个问题,确保永远不会有超过5个元素。

        3
  •  1
  •   Mureinik    9 年前

    狗的公共方法 想象上的 添加跳蚤是 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 而不是 阵列列表 ,教科书解决方案将使用 Collections.unmodifiableList :

    public List<Flea> getDogFlea() {
         return Collections.unmodifiableList(fleas);
    }
    
        4
  •  0
  •   Amit Bhati    9 年前

    在hostFlea方法中:-

    在将Flea对象添加到Dog类中的跳蚤ArrayList之前,只需检查这个数组列表的大小。

    喜欢:-

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