-2
當然,因爲回報是字符串類型的,我不能返回一個空的結果,但我也可以一個字符串變量並不適用於它,然後傳遞經過的結果,所以我怎樣才能返回我的while循環的結果作爲一個字符串?我試着將字符串變量,這顯然不工作,然後創建字符串變量,然後將它們串聯到root.inorder的東西,但不會因爲操作數+的工作。如何從通用型返回一個字符串
public String toString() {
ArrayIterator<T> iter = new ArrayIterator<T>();
String result = "";
while (iter.hasNext())
root.inorder(iter);
return null;
}
package javafoundations;
public class Project10 {
private static LinkedBinaryTree<Integer> tree;
public static void main (String[] args) {
LinkedBinaryTree<Integer> n2, n3, n4, n5, n6, n7, n8 ,n9, n10, n11, n12, n13;
Integer e1 = 10;
Integer e2 = 6;
Integer e3 = 19;
Integer e4 = 4;
Integer e5 = 8;
Integer e6 = 16;
Integer e7 = 20;
Integer e8 = 3;
Integer e9 = 5;
Integer e10 = 7;
Integer e11 = 9;
Integer e12 = 15;
Integer e13 = 17;
n8 = new LinkedBinaryTree<Integer>(e8);
n9 = new LinkedBinaryTree<Integer>(e9);
n4 = new LinkedBinaryTree<Integer>(e4, n8, n9);
n10 = new LinkedBinaryTree<Integer>(e10);
n11 = new LinkedBinaryTree<Integer>(e11);
n5 = new LinkedBinaryTree<Integer>(e5, n10, n11);
n12 = new LinkedBinaryTree<Integer>(e12);
n13 = new LinkedBinaryTree<Integer>(e13);
n6 = new LinkedBinaryTree<Integer>(e6, n12, n13);
n7 = new LinkedBinaryTree<Integer>(e7);
n3 = new LinkedBinaryTree<Integer>(e3, n6, n7);
n2 = new LinkedBinaryTree<Integer>(e2, n4, n5);
tree = new LinkedBinaryTree<Integer>(e1, n2, n3);
System.out.println("find: " + tree.find(e13));
System.out.println("Contains: " + tree.contains(e1));
System.out.println("empty?: " + tree.isEmpty());
System.out.println("size: " + tree.size());
System.out.println("getRoot: " + tree.getRootElement());
System.out.println(tree);
System.out.println(tree.getLeft());
System.out.println(tree.getLeft().toString());
System.out.println(tree.levelorder());
System.out.println(tree.inorder());
System.out.println(tree.preorder());
System.out.println(tree.postorder());
/*StudentRecord<String, String> student1 = new StudentRecord<String, String>("Mike Litoris", 1235, "Sophomore \n");
StudentRecord<String, String> student2 = new StudentRecord<String, String>("Graham Swallows", 1236, "Freshman\n");
StudentRecord<String, String> student3 = new StudentRecord<String, String>("Gift Sales", 1237, "Senior\n");
StudentRecord<String, String> student4 = new StudentRecord<String, String>("Chew Kok", 1238, "Junior\n");
StudentRecord<String, String> student5 = new StudentRecord<String, String>("Bob Dylan", 5678, "Senior\n");
BTNode<StudentRecord<String, String>> student = new BTNode<StudentRecord<String, String>>(student1);
student.setLeft(student1);*/
}
}
我不知道我理解你的問題。你定義了某種參數化的容器,並且想爲該容器實現toString()嗎? – 2012-04-18 23:21:41
我有了加載對象樹司機和這的toString需要打印那些拿出來給我 – Tigh 2012-04-18 23:36:13
所以,如果我理解你:這是你自己的T',這也定義了'容器'的toString()'方法類'ArrayIterator'。你想'的toString()'到容器(看起來像一棵樹)的全部內容的格式,但不知道如何聚合所有的字符串化的內容在一個地方以正確的順序。我對麼? –
2012-04-18 23:42:46