Java 将对象与列表中的其他对象进行比较
我遇到了一个奇怪的问题。我正在处理属于不同类的不同对象。我要做的是,使用递归算法,查看一个对象是否与另一个对象内的另一个对象相等:Java 将对象与列表中的其他对象进行比较,java,list,compare,Java,List,Compare,我遇到了一个奇怪的问题。我正在处理属于不同类的不同对象。我要做的是,使用递归算法,查看一个对象是否与另一个对象内的另一个对象相等: private static void foundObject(EObject obj, EObject set) { if(EcoreUtil.equals(obj, set)){ System.out.println("The objects are the same: \n\t"+obj+"\n\t"+set)
private static void foundObject(EObject obj, EObject set) {
if(EcoreUtil.equals(obj, set)){
System.out.println("The objects are the same: \n\t"+obj+"\n\t"+set);
return;
} else {
System.out.println("The objects are not the same: \n\t"+obj+"\n\t"+set+" "+set.eContents());
if(set.eContents().size()>0){
for(EObject child: set.eContents()){
foundObject(obj, child);
}
}
}
}
我想看看赋值类的对象是否在组内:
- 分配
- 组[任务,关键字]
> The objects are not the same:
Assignment
Group [Assignment, Keyword]
The objects are the same:
Assignment
Assignment
The objects are not the same:
Assignment
Keyword
这是正确的。问题是,当初始组获得另一个赋值元素时
组[作业,关键字,作业]
该算法看不到相应的匹配,并提供如下输出:
The objects are not the same:
Assignment
Group [Assignment, Keyword, Assignment]
The objects are not the same:
Assignment
Assignment
The objects are not the same:
Assignment
RuleCall
The objects are not the same:
Assignment
Keyword
The objects are not the same:
Assignment
Assignment
The objects are not the same:
Assignment
RuleCall
但是在我的例子中,第二对元素是相同的(这就是为什么我使用ECoreUtil.equals)。
知道为什么会发生这种情况吗?格式不好。没有人会读你的代码。至少如果你没有把它变成可读的格式,如果你所有的调试都在你发布的代码之外,人们将不知道发生了什么。