创建Java代码以在nodeTree

时间:2018-09-21 12:09:09

标签: java

我正在尝试编写如下代码输出:

NodeA(id:7000,parentID:0)

NodeB(id:123,parentID:7000)

NodeC(id:9,parentID:123)

NodeD(id:2,parentID:7000)

NodeE(id:25,parentID:7000)

NodeF(id:3,parentID:0)

NodeG(id:10,parentID:3)

其中childs parentId是相同的父节点ID,下面是我的代码

import java.util.ArrayList;
import java.util.Scanner;


public class CustomArrayList {


    // custom class which has data type 
    // class has defined the type of data ArrayList 
    // size of input 3
    int n = 3;
    // the custom datatype class    
    class Data {
        // global variables of the class
        int id;
        int parentId;
        String label;
         // constructor has type of data that is required 
        public Data(int id, int parentId, String label) {
             // initialize the input variable from main 
            // function to the global variable of the class 
            this.id = id;
            this.parentId = parentId;
            this.label = label;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){
        System.out.print("Enter how many friends: ");   
        int id[] = {sc.nextInt()};

        int parentId[];
        System.out.println("please enter the of the node");
        String label[] = {sc.nextLine()};

        CustomArrayList customList = new CustomArrayList();

        customList.addData(id, parentId, label);
        }
    }
    public void addData(int id[], int parentId[], String label[]) {
        ArrayList<Data>list = new ArrayList<>();

        for(int i = 0; i < n; i++){
            list.add(new Data(id[i], parentId[i], label[i]));

        }
        printValues(list);


    }
    private void printValues(ArrayList<Data> list) {

        for(int i=0; i<n; i++){
            Data data = list.get(i);

            System.out.println(data.id + "" + data.parentId + "" + data.label);
        }

    }
}

它应该提示用户用户输入节点的标签和ID,之后将它们显示在层次结构中

我感谢大家阅读此书

1 个答案:

答案 0 :(得分:0)

CustomArrayList类意味着包含Data个项目的列表,因此ArrayList<Data>list变量需要是CustomArrayList类的类成员。

您想读取用户输入的Data项目,然后将该数据添加到列表中。然后重复,直到每个项目都已阅读。

public class CustomArrayList {
    // the custom datatype class    
    class Data {
        // Code removed for brevity.....
    }

    // This is now a class member
    ArrayList<Data>list = new ArrayList<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Just create this once (outside the loop)
        CustomArrayList customList = new CustomArrayList();

        System.out.print("Enter how many friends: ");   
        int count = sc.nextInt();

        // You asked how many, so use a for loop
        for (int i = 0; i < count; i++) {
            // Arrays not needed here as we are creating one at a time
            // TODO: Add error handling for bad user input
            // Get the data
            System.out.println("please enter the id of the node");
            int id = sc.nextInt();
            System.out.println("please enter the parent id of the node");
            int parentId = sc.nextInt();
            // see https://stackoverflow.com/a/13102066/669576
            // for why there is a nextLine call here
            sc.nextLine();
            System.out.println("please enter the label of the node");
            String label = sc.nextLine();

            // Add single item to list
            customList.addData(id, parentId, label);
        }

        // Now we have all the data, print it
        customList.printValues();
    }

    // Not arrays
    public void addData(int id, int parentId, String label) {
        // Just adding a single - no need to loop
        //for(int i = 0; i < n; i++){
        list.add(new Data(id, parentId, label));
        // printValues(list); not here
    }
    //private void printValues(ArrayList<Data> list) {
    public void printValues() {
        for (int i = 0; i < list.size(); i++){
            Data data = list.get(i);
            // Maybe override toString() in the Data class
            System.out.println(data.id + " " + data.parentId + " " + data.label);
        }
    }
}

我刚刚解决了您的代码问题。这不是树结构。您将需要自己做。但是,既然您可以正确输入数据,那么将ArrayList更改为树应该不会太困难。 (提示:Data类将需要保留对其父和/或子Data对象的引用。)