如何从泛型类型返回一个字符串

时间:2012-04-18 23:17:02

标签: java generics tostring

当然因为return是string类型我不能返回void结果,但是我也不能将一个字符串变量应用到它然后通过结果传递,所以我怎样才能将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);*/

    }
}

1 个答案:

答案 0 :(得分:1)

刚做

有什么问题
public String toString() { 

    ArrayIterator<T> iter = new ArrayIterator<T>(); 

    String result = "";

    while (iter.hasNext())
        result += root.inorder(iter).toString();

    return result;        
}

假设root.inorder(iter)返回T