Ich bin neu mit Sammlungen. Ich habe folgende Klasseretrive alle Kinder von N Anzahl der Ebenen Von Sammlung
class ParantCategory {
int id;
String name;
int pid;
public ParantCategory(int id, String name, int pid) {
this.id = id;
this.name = name;
this.pid = pid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
}
und meine Hauptmethode Klasse ist als
public class HierachiDemo {
static ArrayList<ParantCategory> al = new ArrayList<>();
public static void main(String[] args) {
al.add(new ParantCategory(1, "000", 0));
al.add(new ParantCategory(2, "A1", 1));
al.add(new ParantCategory(3, "B1", 1));
al.add(new ParantCategory(4, "A11", 2));
al.add(new ParantCategory(5, "A12", 2));
al.add(new ParantCategory(6, "A13", 2));
al.add(new ParantCategory(7, "B11", 3));
al.add(new ParantCategory(8, "B12", 3));
al.add(new ParantCategory(9, "B13", 3));
al.add(new ParantCategory(10, "A111", 4));
al.add(new ParantCategory(11, "A112", 4));
al.add(new ParantCategory(12, "A113", 4));
HierachiDemo h = new HierachiDemo();
//function call here
}
}
ich Elemente in Arraylist gespeichert haben. Nun ist meine Frage, ich brauche Rekursionsfunktion in dem ich Name übergeben werde und es mir alle Kinder aus n Anzahl von Ebenen geben wird. zum Beispiel: Wenn ich A1 als Name gebe, dann sollte es mir alle Kinder wie A11, A12, A13 zurückgeben, und auch wenn Subkind von A11, A12, A13, die auch angezeigt werden sollte. Ich brauche das Endergebnis als A11, A12, A13, A111, A112, A113.
Sind Eltern und Kind durch ID und PID verwandt? – matt
Warum versuchen Sie, eine hierarchische Struktur in einer Liste (flach) darzustellen? – Spotted