Ich habe die folgenden Klassen:baumartige Struktur
public class Model {
protected String name;
protected ArrayList<Model> children;
public Model(){
children = new ArrayList<Model>();
}
public void addChild(Model node) {
children.add(node);
}
//other code here
}
public class cPackage extends Model{
public cPackage() {
super();
}
//other code here
}
public class cClass extends Model{
public cClass() {
super();
}
//other code here
}
public class cMethod extends Model{
public cMethod() {
super();
}
//other code here
}
public void tryme(Object[] objs){
String values = (String)objs[0];
Model root = null;
astNode = ax.getDeclaringNodeInProject(handles, bindings);
if(astNode instanceof TypeDeclaration){
TypeDeclaration cl = (TypeDeclaration)astNode;
root = new cClass();
root.setName(cl.getName().getFullyQualifiedName());
}else if(astNode instanceof MethodDeclaration){
MethodDeclaration me = (MethodDeclaration)astNode;
root = new cMethod();
List mod = me.parameters();
String vars ="";
...
root.setName(me.getName().getFullyQualifiedName() + "(" + vars + ")");
}
Model currentNode = null;
Model prevNode = root;
for(int i = 1 ; i < objs.length; i++){
if(astNode instanceof PackageDeclaration){
PackageDeclaration pka = (PackageDeclaration)astNode;
p8.setName(pka.getFullyQualifiedName());
if(currentNode == null) {
currentNode = p8;
prevNode.addChild(currentNode);
}
prevNode = currentNode;
}else if(astNode instanceof MethodDeclaration){
MethodDeclaration me = (MethodDeclaration)astNode;
...
m8.setName(me.getName().getFullyQualifiedName() + "(" + vars + ")");
if(currentNode == null) {
currentNode = m8;
prevNode.addChild(currentNode);
}
prevNode = currentNode;
}else if(astNode instanceof TypeDeclaration){
TypeDeclaration cl = (TypeDeclaration)astNode;
c8.setName(cl.getName().getFullyQualifiedName());
if(currentNode == null) {
currentNode = c8;
prevNode.addChild(currentNode);
}
prevNode = currentNode;
}
}
}
Sorry, wenn es fehlt "{" oder "}". Was ich versuche hier zu tun ist, ich habe eine Daten vom Server wie folgt:
alle in Arrays. Ich möchte etwas wie eine baumartige Struktur aufbauen, wobei von links gelesen wird:
Jedes Element wird von dem Element, das ihm folgt, entfernt. Ich durchlaufe die Daten, um es zu tun. Zum Beispiel möchte ich so etwas wie bekommen:
+School
Chair
Table
Chalk
+Apple
Peer
Mango
Melon
Aber wenn ich den obigen Code ausführen bekomme ich nur die ersten beiden Elemente wie:
+School
Chair
+Apple
Peer
und die folgenden Elemente zeigen nicht auf.
Kann mir jemand helfen, wo ich falsch mache.
Danke.
was ist objs? Wie wird ich benutzt? Hast du ein rekursives Parsing probiert? –
Ihre Frage und Code passt nicht zusammen;) Was ist ein Paket/Methode/TypeDeclaration, wie beziehen sie sich auf die anderen Methoden, und warum verwenden Sie den gleichen "astNode" in jeder Iteration der for-Schleife? – Christoffer