gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:20:08 27 4
gpt4 key购买 nike

enter image description here

这是我的汽车类(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 + '}';
}
}

这是我的 Owner 类:

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.StackOverflowError
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)

……

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

我如何确保在创建新车时车主的 Collection 也会发生变化。如果一辆车有一个车主,但车主并不 Collection 这辆车,这是没有任何意义的。

最佳答案

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

汽车类

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

在所有者类别中

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 类的 addCar() 方法,这样就可以了。但是,为什么 Owner 类的 addCar() 方法再次将所有者设置为自身?

我认为存在逻辑错误。如果删除 c.setOwner(this) 行,它就可以正常工作。

关于java - 典型 1 对 n 关系的 Stackoverflow 异常(递归),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48329541/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com