创建 Java 代码以在节点树中显示用户输入数据



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

节点 A (id: 7000, 父 ID: 0(

节点 B (id: 123, 父 ID: 7000(

NodeC (id: 9, parentID: 123(

NodeD (id: 2, parentID: 7000(

NodeE (id: 25, parentID: 7000(

节点 F (id: 3, 父 ID: 0(

NodeG (id: 10, parentID: 3(

其中子节点 ID 与父节点 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的标签,之后它将在层次结构中显示它们

谢谢大家阅读这篇文章

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对象的引用(。

最新更新