i am trying to write code outputs something like this:
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)
where the childs parentId is the same a parent node id and below is my code
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);
}
}
}
it should prompt the user user enter label of the node and id after which it will display them in a hierachy
i thank you all for reading this