典型 1-to-n 关系的 Stackoverflow 异常(递归)

Stackoverflow exception (Recursion) for typical 1-to-n relationship

这是我的车class:

public class Car {
private int FGNr;
private String name;
private String type;
private Owner owner;

private static ArrayList<Integer> allCarIds = new ArrayList<>();

public Car(int FGNr, String name, String type, Owner o) throws Exception {
    setFGNr(FGNr);
    setName(name);
    setType(type);
    setOwner(o);
}



public int getFGNr() {
    return FGNr;
}


public void setFGNr(int FGNr) throws Exception{
    this.FGNr = FGNr;
    if(allCarIds.contains(this.FGNr))
        throw new Exception("FGNr already excists!! ");
     allCarIds.add(this.FGNr);}

public String getName() {
    return name;
}

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

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Owner getOwner() {
    return owner;
}

public void setOwner(Owner owner) throws Exception{ 
    owner.addCar(this);
    this.owner = owner;
}

@Override
public int hashCode() {
    int hash = 7;
    hash = 73 * hash + this.FGNr;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Car other = (Car) obj;
    if (this.FGNr != other.FGNr) {
        return false;
    }
    return true;
}



@Override
public String toString() {
    return "Car{" + "FGNr=" + FGNr + ", name=" + name + ", type=" + type + ", owner=" + owner + '}';
  }   
}

这是我的主人class:

public class Owner {
private String SVNr;
private String name;
HashSet<Car> allCars = new HashSet<>();
private static ArrayList<String> allOwnerSVNs = new ArrayList<>();

public Owner(String SVNr, String name) throws Exception{
    setSVNr(SVNr);
    setName(name);
}

public void addCar(Car c) throws Exception{
    if(allCars.contains(c))
        throw new Exception("this user has already this car");
    if(c.getOwner()!=null)
        throw new Exception("this car belongs to other owner");

    c.setOwner(this);
    allCars.add(c);
}

public String getSVNr() {
    return SVNr;
}

public void setSVNr(String SVNr) throws Exception{
    this.SVNr = SVNr;
     if(allOwnerSVNs.contains(this.SVNr))
        throw new Exception("SVNg already excists!! ");
     allOwnerSVNs.add(this.SVNr);
}

public String getName() {
    return name;
}

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

public HashSet<Car> getAllCars() {
    return allCars;
}

@Override
public int hashCode() {
    int hash = 5;
    hash = 41 * hash + Objects.hashCode(this.SVNr);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Owner other = (Owner) obj;
    if (!Objects.equals(this.SVNr, other.SVNr)) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Owner{" + "SVNr=" + SVNr + ", name=" + name + ", allCars=" + allCars + '}';
    }    
}

这是我的主要内容:

 try {
        Owner o1 = new Owner("0001","Owner1");
        Owner o2 = new Owner("0002","Owner2");

        Car c1 = new Car(1,"Model S", "Tesla",o1);
        Car c2 = new Car(2,"Model 3", "Tesla",o2);
        Car c3 = new Car(3,"TT", "Audi",o2);


    } catch (Exception ex) {
        System.out.println("error:"+ex.getMessage());

    }

因此,在尝试创建新汽车时出现此错误:

Exception in thread "main" java.lang.WhosebugError
at java.util.HashMap.containsKey(HashMap.java:595)
at java.util.HashSet.contains(HashSet.java:203)
at pkgData.Owner.addCar(Owner.java:28)
at pkgData.Car.setOwner(Car.java:63)

............

这是一个递归错误,但我不知道如何修复它。如果我创建一辆新车,显然我必须将 Car 添加到车主数组列表中。如果我调用 addCar 函数,该函数将调用 getOwner 函数。这是一个无尽的循环。

我如何确保在创建新车时车主的 collection 也会更改。车有车主但车主没有车在他的 collection.

是没有任何意义的

通常向容器中添加元素应该在容器本身中完成。 在您的示例中,Owner 是容器,Car 是元素。

例如 java.awt.Containerjava.awt.Component

不要从 Car.setOwner 调用 owner.addCar(this);

Owner 将汽车添加到列表中(您已经这样做了)并将自己设置为 Car 的所有者。

public void setOwner(Owner owner) throws Exception{ 
    owner.addCar(this); //remove this line
    this.owner = owner;
}

如您所见,这两个函数陷入无限循环。

在车里class

public void setOwner(Owner owner) throws Exception{ 
    owner.addCar(this);
    this.owner = owner;
}

并在所有者中 Class

public void addCar(Car c) throws Exception{
    if(allCars.contains(c))
        throw new Exception("this user has already this car");
    if(c.getOwner()!=this && c.getOwner()!=null)
        throw new Exception("this car belongs to other owner");

    c.setOwner(this);
    allCars.add(c);
}

小车设置自己的owner,把自己发送给Ownerclass'addCar()方法,就OK了。但是,为什么 Owner class' addCar() 方法将所有者设置为自己 again ?

我觉得有逻辑错误。如果你删除 c.setOwner(this) 行,它工作正常。