-2
返り値は型文字列なので返すことはできませんが、文字列変数を適用して結果を渡すことはできません。文字列としての私のwhileループの結果?私は明らかに動作しない文字列変数を適用しようとした後、文字列変数を作成し、それらをroot.inorderのものに連結しますが、それはオペランド+のために動作しません。ただroot.inorder(iter)
戻りT
を想定しジェネリック型から文字列を返す方法
public String toString() {
ArrayIterator<T> iter = new ArrayIterator<T>();
String result = "";
while (iter.hasNext())
result += root.inorder(iter).toString();
return result;
}
を行うと間違って何
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()を実装したいのですか? –
私はオブジェクトを持つツリーをロードしたドライバを持っています。このtoStringはそれらを私に出力する必要があります – Tigh
これはあなたのことを理解していれば、これは 'T 'のコンテナ内の' toString() 'メソッドです。クラス 'ArrayIterator'です。あなたは 'toString()'を使ってコンテナの内容全体をフォーマットします(ツリーのように見える)が、文字列化されたすべてのコンテンツを正しい順序で1か所に集める方法は分かりません。私は正しいですか? –