我在java中使用For循环做错了什么?

时间:2022-09-20 18:57:29

I have been looking around for some answers to this question and I just can't seem to figure it out. I think that the issue seems to be with the 'scope' of my code but I don't know what I'm doing wrong and I could really use some help. I'm very new to Java.

我一直在寻找这个问题的答案,我似乎无法弄明白。我认为问题似乎与我的代码的“范围”有关,但我不知道我做错了什么,我真的可以使用一些帮助。我是Java的新手。

My goal

  1. Test to see if an object already exists in an array before adding it.

    在添加对象之前测试数组中是否已存在对象。

  2. If the object already exists in the array, return null.

    如果对象已存在于数组中,则返回null。

  3. If it doesn't exist in the array, create it and then return it.

    如果数组中不存在,则创建它然后返回它。

The Code

    public Business addBusiness(String person, String business, String location) {
    int id = 0;

    for (Business business : businesses ) {
        if (business.getPerson().equals(person)) {
            if (business.getBusiness().equals(business)) {
                if (business.getLocation().equals(location)) {
                    return null;
                }
            }
        }
    }
    Business newBusiness = new Business(person, business, location, id);
    return newBusiness;
}

What happens when I run it

运行时会发生什么

When I run the code it will just happily create object after object with the same details. This leads me to believe that there is a problem with my logic. I was expecting that when it returned null, that would be the end of it.

当我运行代码时,它将很乐意在具有相同细节的对象之后创建对象。这让我相信我的逻辑存在问题。我期待当它返回null时,那将是它的结束。

I would appreciate any tips you can give me on solving this problem.

我很感激你可以给我解决这个问题的任何提示。

EDIT: The 'Business' class

编辑:'商务'课程

public class Business {     
// Attributes //
private String person, business, location;
private int id;

// Constructor //

public Business(String person, String business, String location, int id) {
    this.person = person;
    this.business = business;
    this.location = location;
    this.id = id;
}

// Getters //

public String getPerson() {
    return person;
}

public String getBusiness() {
    return business;
}

public String getLocation() {
    return location;
}

public int getId() {
    return id;
}
}

2 个解决方案

#1


1  

Try

public Business addBusiness(String person, String business, String location) 
{
    int id = 0;

    for (Business b : businesses ) {
        if (b.getPerson().equals(person)) {
            if (b.getBusiness().equals(business)) {
                if (b.getLocation().equals(location)) {
                    return null;
                }
            }
        }
    }
    Business newBusiness = new Business(person, business, location, id);
    return newBusiness;
}

You were masking the parameter business from the function's signature with the object you're iterating over in the loop. Renaming this to b avoids the issue.

您正在使用您在循环中迭代的对象从函数的签名中屏蔽参数业务。将其重命名为b可避免此问题。

#2


1  

Okay -- so I think that I figured it out. Thank you so much to everyone for your help on this matter.

好的 - 所以我认为我弄清楚了。非常感谢大家对此事的帮助。

public Business addBusiness(String person, String business, String location) 
{
int id = 0;

for (Business business : businesses ) {
    if (business.getPerson().equals(person)) {
        if (business.getBusiness().equals(business)) {
            if (business.getLocation().equals(location)) {
                return null;
            }
        }
    }
}
Business newBusiness = new Business(person, business, location, id);
return newBusiness;
}

The issue was caused because I never actually added my newBusiness object into the array once it was created. So every time it ran it would never match an existing entry because there were no entries in the array.

这个问题是因为我从未在创建过程中将newBusiness对象实际添加到数组中。所以每次运行它都不会匹配现有的条目,因为数组中没有条目。

The correct code should be something like:

正确的代码应该是这样的:

public Business addBusiness(String person, String business, String location) 
{
int id = 0;

for (Business b : businesses ) {
    if (b.getPerson().equals(person)) {
        if (b.getBusiness().equals(business)) {
            if (b.getLocation().equals(location)) {
                return null;
            }
        }
    }
}
Business newBusiness = new Business(person, business, location, id);
b.add(newBusiness); // This was the missing line, now it seems to work fine.
return newBusiness;
}

Clearly, I still have a lot to learn.

显然,我还有很多东西需要学习。

#1


1  

Try

public Business addBusiness(String person, String business, String location) 
{
    int id = 0;

    for (Business b : businesses ) {
        if (b.getPerson().equals(person)) {
            if (b.getBusiness().equals(business)) {
                if (b.getLocation().equals(location)) {
                    return null;
                }
            }
        }
    }
    Business newBusiness = new Business(person, business, location, id);
    return newBusiness;
}

You were masking the parameter business from the function's signature with the object you're iterating over in the loop. Renaming this to b avoids the issue.

您正在使用您在循环中迭代的对象从函数的签名中屏蔽参数业务。将其重命名为b可避免此问题。

#2


1  

Okay -- so I think that I figured it out. Thank you so much to everyone for your help on this matter.

好的 - 所以我认为我弄清楚了。非常感谢大家对此事的帮助。

public Business addBusiness(String person, String business, String location) 
{
int id = 0;

for (Business business : businesses ) {
    if (business.getPerson().equals(person)) {
        if (business.getBusiness().equals(business)) {
            if (business.getLocation().equals(location)) {
                return null;
            }
        }
    }
}
Business newBusiness = new Business(person, business, location, id);
return newBusiness;
}

The issue was caused because I never actually added my newBusiness object into the array once it was created. So every time it ran it would never match an existing entry because there were no entries in the array.

这个问题是因为我从未在创建过程中将newBusiness对象实际添加到数组中。所以每次运行它都不会匹配现有的条目,因为数组中没有条目。

The correct code should be something like:

正确的代码应该是这样的:

public Business addBusiness(String person, String business, String location) 
{
int id = 0;

for (Business b : businesses ) {
    if (b.getPerson().equals(person)) {
        if (b.getBusiness().equals(business)) {
            if (b.getLocation().equals(location)) {
                return null;
            }
        }
    }
}
Business newBusiness = new Business(person, business, location, id);
b.add(newBusiness); // This was the missing line, now it seems to work fine.
return newBusiness;
}

Clearly, I still have a lot to learn.

显然,我还有很多东西需要学习。