有点难以解释。在此示例代码中:
public class SomeClass
{
private String id;
private String parent;
public SomeClass(String id, String parent)
{
this.id = id;
this.parent = parent;
}
public String getParent()
{
return parent;
}
}
List<SomeClass> someList = new ArrayList();
someList.add(new SomeClass("Test1", "none"));
someList.add(new SomeClass("Test2", "none"));
someList.add(new SomeClass("Test1Mem1", "Test1"));
someList.add(new SomeClass("Test2Mem1", "Test2"));
someList.add(new SomeClass("Test1Mem1Obj1", "Test1Mem1"));
我想创建一个函数来获取包含&#34; parent&#34;的&#34;层次结构的所有对象。领域。因此,例如,如果我查找&#34; Test1Mem1Obj1&#34;,它应该给我&#34; {Test1Mem1,Test1}&#34;的值。如果我查找&#34; Test2Mem1&#34;,它应该给我&#34; {Test2}&#34;的值。基本上是获取父级父级的父级,依此类推。由于语言障碍,我很抱歉这个解释。我希望有人可以帮助我。谢谢!
我有一个临时的脏解决方案,你可以看出为什么这不好。
if(someObj.getParent() != null)
{
result.add(someObj.getParent());
if(someObj.getParent().getParent() != null)
{
result.add(someObj.getParent().getParent());
if(someObj.getParent().getParent().getParent() != null)
{
result.add(someObj.getParent().getParent().getParent());
}
}
}
答案 0 :(得分:1)
如果您getParent()
可以SomeClass
而不是String
,那么这很简单:
public boolean isDescendantOf(String parentName) { // part of SomeClass
SomeClass parent = this.parent;
while (!parent.id.equals("none")) { // or null check
if (parent.id.equals(parentName)) {
return true; // found a parent named parentName
}
}
return false; // eventually reached a parentless parent and never found one matching parentName
}
但也许这是不可能的。如果你可以把东西放在地图上,就像这样:
Map<String, SomeClass> map = new HashMap<>(); // map from parent name to SomeClass
map.put("Test1", new SomeClass("Test1", "none"));
map.put("Test2", new SomeClass("Test2", "none"));
map.put("Test1Mem1", new SomeClass("Test1Mem1", "Test1"));
map.put("Test2Mem1", new SomeClass("Test2Mem1", "Test2"));
map.put("Test1Mem1Obj1", new SomeClass("Test1Mem1Obj1", "Test1Mem1"));
然后你可以像这样循环使用递归:
public boolean isDescendentOf(SomeClass child, String parentName) {
SomeClass parent = map.get(child.parent);
if (parent == null) {
throw new RuntimeException("Warning: parent doesn't exist!");
}
if (parent.id.equals(parentName)) {
return true;
} else {
return isDescendentOf(parent, parentName);
}
}
如果要填充给定元素的所有父元素的列表,请调用如下函数:
public static void PopulateParents(List<String> parents, Map<String, ClassTest> nodes, ClassTest child) {
if (child.parent.equals("none")) {
return;
}
ClassTest parent = nodes.get(child.parent);
if (parent == null) {
throw new RuntimeException("No parent exists called " + child.parent);
}
parents.add(parent.id);
PopulateParents(parents, nodes, parent);
}