2013-04-21 2 views
1

내 자바 코드에서 트리의 노드가 노드 인 트리를 만들었습니다. 노드의 이름, 속성 및 유형이 인 노드이 있습니다. 트리를 저장하기 위해 db4o을 사용하고 있습니다. 나는 단순히 나무의 루트 노드를 저장함으로써 그렇게하고있다. 그러나 db4o는 객체 노드의 모든 자식 노드를 저장하지 않는다는 것을 알게되었습니다. 데이터베이스에서 루트를 검색하고 트리를 탐색 할 때 트리의 최대 3 레벨까지만 트래버스 할 수 있습니다. 낮은 수준의 자식 노드가 손실 된 것 같습니다. 누군가 노드를 잃지 않도록 나를 도울 수 있습니까? 고맙습니다. 다음은 루트 객체를 저장하여 db4o에 트리를 저장

내 코드입니다 :

Node node1= new Node("root","this is the root",new ArrayList<Node>()); 
Node node2= new Node("zaid","123",new ArrayList<Node>()); 
Node node3= new Node("saad","999",new ArrayList<Node>());   
Node node4= new Node("safia","555",new ArrayList<Node>()); 
Node node5= new Node("ahmad","000",new ArrayList<Node>()); 

node1.getChildren().add(node2); 
node2.getChildren().add(node3); 
node3.getChildren().add(node4); 
node4.getChildren().add(node5); 

ObjectContainer db= Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(),"db"); 
db.store(node1); 

Node node= new Node("root",null,null); 
List<Node> result= db.queryByExample(node); 
node= result.get(0); 
System.out.println(node.getName() 
     +","+node.getChildren().get(0).getName() 
     +","+node.getChildren().get(0).getChildren().get(0).getName() 
     +","+node.getChildren().get(0).getChildren().get(0).getChildren().get(0).getName()); 

내 말은 코드의 마지막 줄에서 예외를 얻고 다음

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
+0

당신은 3 레벨의 나무를 가로 지르고 있다고 생각합니까? – Sridhar

+0

이 정보가 정확한지 확신 할 수 없습니다 (오류는 항상 3 레벨을 통과 한 후에 발생합니다). 그러나 트리 탐색의 시작 지점 인 트리의 4 레벨에서 노드를 표시하려고 시도한 후에 오류가 발생했는지 완전히 확신합니다. 뿌리는 모든 어린이들에게 반복됩니다. –

+1

커밋을 수행하지 않았습니까? – vels4j

답변

0

그것은 나를 위해 일할 때, 나는 당신을 보여주는거야 COMPLET 작업 예 : 다음

import java.util.*; 

class Node 
{ 
    String _name; 
    public String getName() {return _name;} 
    public void setName(final String name) { _name = name;} 

    String _value; 
    public String getValue() {return _value;} 
    public void setValue(final String value) { _value = value;} 

    List<Node> _children; 
    public List<Node> getChildren() {return _children;} 
    public void setChildren(final List<Node> children) { _children = children;} 

    Node(final String name, final String value, final List<Node> children) 
    { 
    setName(name); 
    setValue(value); 
    setChildren(children); 
    } 
} 

는 메인 클래스를 정의 :

import java.util.*; 
import com.db4o.*; 
import com.db4o.query.*; 
import com.db4o.ta.Activatable; 

class test 
{ 
    public static void main(String[] argv) 
    { 
Node node1= new Node("root","this is the root",new ArrayList<Node>()); 
Node node2= new Node("zaid","123",new ArrayList<Node>()); 
Node node3= new Node("saad","999",new ArrayList<Node>());   
Node node4= new Node("safia","555",new ArrayList<Node>()); 
Node node5= new Node("ahmad","000",new ArrayList<Node>()); 

node1.getChildren().add(node2); 
node2.getChildren().add(node3); 
node3.getChildren().add(node4); 
node4.getChildren().add(node5); 

ObjectContainer db= Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(),"db"); 
db.store(node1); 

Node node= new Node("root",null,null); 
List<Node> result= db.queryByExample(node); 
node= result.get(0); 
System.out.println(
      node.getName() 
     +","+node.getChildren().get(0).getName() 
     +","+node.getChildren().get(0).getChildren().get(0).getName() 
     +","+node.getChildren().get(0).getChildren().get(0).getChildren().get(0).getName()); 
    } 

} 

및 빌드/실행은 다음과 같이 수행 할 수 있습니다

javac -classpath "db4o-8.0.249.16098-all-java5.jar:." *.java 
java -classpath "db4o-8.0.249.16098-all-java5.jar:." test 

당신은 db4o는 더 많은 정보를 가질 수 있습니다 -> 문서 -> 튜토리얼 8.0. 8.1이 릴리스 된 동안 특별한 튜토리얼은 없습니다.

+0

여전히 문제를 실험하는 경우 기본 구성 (configuration.common(). activationDepth (10);)을 사용하는 대신 구성에서 "활성화 깊이"를 강제로 설정할 수 있습니다. "투명 활성화"는 다음 질문 일 것 같아 보이기도합니다. – Galigator

관련 문제