首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java ArrayList:检查id是否在列表中

Java ArrayList:检查id是否在列表中
EN

Stack Overflow用户
提问于 2019-02-25 08:47:41
回答 2查看 653关注 0票数 1

我在用Java解决这个基本问题时遇到了困难。我有一个5个动物笼子的数组,每个笼子都有一个ID。当我尝试验证一个笼子里是否已经有一个动物时,检查只对前两个笼子有效,后面的都是错误的。

这是主要的:

代码语言:javascript
复制
    Cat picasso = new Cat ("Cat", "Picasso");
    Cat athena = new Cat("Cat", "Athena");
    Dog thunder = new Dog ("Dog", "Thunder");
    Dog buffy = new Dog("Dog", "Buffy");

    Pets store = new Pets("Super store");

    store.addToCage(buffy, 1);
    store.addToCage(athena, 1);

这会抛出一条错误消息:"cage is full“

但这个不是:

代码语言:javascript
复制
    store.addToCage(buffy, 1);
    store.addToCage(athena, 2);
    store.addToCage(thunder, 2);

它打印消息(笼子已满),但它仍然将动物添加到笼子2中。任何超过2个笼子的都是有问题的:例如,它打印第三个笼子两次。

以下是类Pets中的addToCage方法:

代码语言:javascript
复制
     class Pets {

        ArrayList<Cage> cages = new ArrayList<Cage>();

     public void addToCage(Animal animal, int cageId){
       int nbrCages = cages.size();
       Cage tmp, cage;

       if(nbrCages == 0){
           cage = new Cage(cageId);
           cage.setAnimal(animal);
           this.cages.add(cage);
       }else{

            for(int i=0; i < nbrCages; i++){
                tmp = cages.get(i);
                if(tmp.getId() == cageId){

                System.out.println("cage is full");
                }else{
                    cage = new Cage(cageId);
                    cage.setAnimal(animal);
                    this.cages.add(cage);

这是打印结果的方法:

代码语言:javascript
复制
    public String displayResults(){
        String results="";
        int nbrCages = cages.size();

        if(nbrCages > 0){
           Cage tmp;
           for(int i=0; i < nbrCages; i++){
             tmp = cages.get(i);
             results += tmp.showCages();
         }

       }else{
       results += "store is empty"; 
    }

       return results;
   }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-25 09:04:59

您的问题是else语句。你应该像这样做一个验证:

代码语言:javascript
复制
public void addToCage(Animal animal, int cageId) {
    int nbrCages = cages.size();
    Cage tmp, cage;

    if (nbrCages == 0) {
        cage = new Cage(cageId);
        cage.setAnimal(animal);
        this.cages.add(cage);
    } else {

        boolean cageInUse = false;

        for (int i = 0; i < nbrCages; i++) {
            tmp = cages.get(i);
            if (tmp.getId() == cageId) {
                System.out.println("cage is full");
                cageInUse = true;
                break;
            }
        }

        if (!cageInUse) {
            cage = new Cage(cageId);
            cage.setAnimal(animal);
            this.cages.add(cage);
        }
    }
}

这不是最好的解决方案,但它解决了您的问题。

票数 2
EN

Stack Overflow用户

发布于 2019-02-25 08:58:39

这就是你正在检查的问题,笼子是空的还是不是nbrCages == 0,而不是笼子有没有空间。

如果animal显示tmp.getId() == cageId,则只需打印cage is full,将其更改为animal is present in cage

代码语言:javascript
复制
public void addToCage(Animal animal, int cageId){
   int nbrCages = cages.size();
   Cage tmp, cage;

   if(nbrCages < 2){     // First check the cage have space or not

    for(int i=0; i < nbrCages; i++){
            tmp = cages.get(i);
            if(tmp.getId() == cageId){     // Now check animal present in cage or not, if present don't add

            System.out.println(" Animal already present in cage ");
            }else{                          // If not present the add the animal to cage
                cage = new Cage(cageId);
                cage.setAnimal(animal);
                this.cages.add(cage);     
   }else{    // If not print cage is full

        System.out.println(" cage is full on animals ");
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54858101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档