Итак, я создал класс для моего общего дерева и класса Node, который вместе с ним. Классы следующие:Trouble Implementing Generic Tree
public class DirectoryTree<E> implements Serializable {
protected Node<E> root;
protected Node<E> child;
public DirectoryTree() {
root = null;
}//end constructor
protected DirectoryTree(Node<E> root) {
this.root = root;
}
public boolean isLeaf() {
return root.children == null;
}
public DirectoryTree<E> getTree() {
if (root != null) {
return new DirectoryTree<E>(root);
}//end if
else {
return null;
}//end else
}
public Node<E> getRoot() {
return root;
}
public Node<E> getChild() {
return child;
}
public String toString() {
return null;
}//end toString()
}
class Node<E> implements Serializable {
protected Node<E> parent;
protected E data;
protected ArrayList<Node<E>> children;
public Node(E data) {
this.data = data;
children = new ArrayList<Node<E>>();
}
public Node<E> getParent() {
return parent;
}
public ArrayList<Node<E>> getChildren() {
return this.children;
}
public int getNumberOfChildren() {
return getChildren().size();
}
public boolean hasChildren() {
return getChildren().size() > 0;
}
public void addChild(Node<E> child) {
int counter = 0;
for (int i = 0; i < children.size(); i++) {
if (child == children.get(i)) {
System.out.println("directory exists");
counter++;
}
}
if (counter == 0) {
children.add(child);
} else {
}
}
public E getData() {
return this.data;
}
public String toString() {
return data.toString();
}
}
Мне интересно, правильно ли установлены мои 2 класса для общего дерева. Кроме того, я не уверен, как реализовать это дерево в программе. Моя программа должна быть программой, которая создает каталоги и файлы и помещает их в дерево. Мой корневой каталог должен иметь имя /. До сих пор мой основной класс (я только начал его):
public class Driver {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Welcome to Fake Unix");
System.out.print("$");
File f = new File("//");
String input = in.nextLine();
DirectoryTree tree = new DirectoryTree(new Node<File>(f));
while (!input.equalsIgnoreCase("exit")) {
String[] command = input.split(" ");
if (command[0].equals("mkdir") && command[1].equals("-p") && command.length == 3) {
File f1 = new File(command[2]);
tree.root.addChild(new Node<File>(f1));
System.out.println("complete");
}//end if
if (input.equals("tree")) {
System.out.println(tree.toString());
}
System.out.print("$");
input = in.nextLine();
}//end while
}//end main
}//end class
Так что я в основном спрашиваю, если мои методы выглядеть правильно в моих деревьев и узлов классов, и я или нет я движется в правильном направлении реализации их в основном классе. Правильно ли я создаю каталоги или неправильно ли синтаксис?
Спасибо.
любой человек ???????? – user3554599
Вы пытались запустить свой класс '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' 'и были проверены, – Foxsly
ну, я не получаю никаких ошибок. но как я могу проверить, создан ли каталог? @Foxsly – user3554599