Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文本输入创建Java树_Java_Arrays_Tree_Hashtable_Treemap - Fatal编程技术网

从文本输入创建Java树

从文本输入创建Java树,java,arrays,tree,hashtable,treemap,Java,Arrays,Tree,Hashtable,Treemap,我需要生成一个表格式数据源的树表示,并且我很难开始。数据是产品层次结构的表示 此输入的树结构如下所示: 到目前为止,我已经用Java编写了一个方法,它读取文本输入(TSV)并生成一个值的2d字符串数组。从这里开始,我不确定如何继续。array类似乎没有定义关系或向树中添加节点所需的方法 Java代码: public class ImportTSV { // Global VARs String[][] tsvArray; List<String> lines; pub

我需要生成一个表格式数据源的树表示,并且我很难开始。数据是产品层次结构的表示

此输入的树结构如下所示:

到目前为止,我已经用Java编写了一个方法,它读取文本输入(TSV)并生成一个值的2d字符串数组。从这里开始,我不确定如何继续。array类似乎没有定义关系或向树中添加节点所需的方法

Java代码:

public class ImportTSV  {   
// Global VARs
String[][] tsvArray;
List<String> lines; 

public static void main(String[] args) throws IOException{
    ImportTSV tsv = new ImportTSV();
    String [][] tmp = tsv.readTSV();
    tsv.arr2tree(tmp);
}

public String[][] readTSV() throws IOException{
    // Read File to lines object
    lines = Files.readAllLines(Paths.get("Input\\TestData_small.txt"), StandardCharsets.UTF_8);

    // Set Array bounds to # of lines in input source
    tsvArray = new String[lines.size()][];

    System.out.println("-- Full 2D Array --");
    // Build the array from TSV source
    for(int i=0; i<lines.size(); i++){
        tsvArray[i] = lines.get(i).split("\t");
        System.out.println(Arrays.deepToString(tsvArray[i]));
    }       
    return tsvArray;
}

public void arr2tree(String[][] arr){
    String[][] data = arr;
    String[] tmp = null;
    System.out.println(" ");
    System.out.println("-- Converting to Tree --");

    // Need a tree
    TreeMap<String, String> tm = new TreeMap<String, String>();

    // Loop through [][] to define each line
    for(int i=0;i<data.length;i++){
        // Then seperate by record
        for(int j=0;j<data[i].length-1;j++){
            // Set comparison variables
            String s1 = data[i][j];
            String s2 = data[i][j+1];

            // See the comparison
            System.out.println("Parent Check: " + s1);
            System.out.println("Child Check: " + s2);


            // Set the relationship in the tree if it doesnt exist
            if(tm.(s1)!=s2){
                tm.put(s1, s2); 
                System.out.println(tm.toString());
            }


        }           
    }       
}   
公共类ImportTSV{
//全局变量
字符串[][]tsvArray;
列出行;
公共静态void main(字符串[]args)引发IOException{
ImportTSV tsv=新ImportTSV();
字符串[][]tmp=tsv.readTSV();
tsv.arr2tree(tmp);
}
公共字符串[][]readTSV()引发IOException{
//将文件读取到对象行
lines=Files.readAllLines(path.get(“Input\\TestData\u small.txt”)、StandardCharsets.UTF\u 8);
//将数组边界设置为输入源中的#行
tsvArray=新字符串[lines.size()][];
System.out.println(“--Full 2D Array-->”);
//从TSV源构建阵列

对于(inti=0;i首先,实现一个可以存储关系的
节点可能是明智的

这是另一个问题的一个很好的例子

public static class Node<T> {
    private T data;
    private Node<T> parent;
    private List<Node<T>> children;
}
公共静态类节点{
私有T数据;
私有节点父节点;
私人名单儿童;
}

在您的情况下,您可能可以将类型
T
更改为
String
,因为您没有说父母是否有固定数量的孩子,所以
列表也适合您。

这看起来很像“为我做家庭作业”问题。我建议您仔细考虑接下来的步骤,并再次尝试。如果您在逻辑或使其工作方面遇到具体问题,请再次发布。谢谢,那么我想在遍历数组时定义一个节点,然后将其保存到映射中?