有如下结构:
Question0 : What is ur name?
Answer0: Tina
Question0.0 : What are your hobbies?
Answer0.0.0 : reading
Question0.0.0.0 :What do you like in reading.?
Answer0.. : .....
Answer0.0.1 : Dancing
Question0.0.0.1 :which dance do you like?
...........
Answer1:Roger
...........same question answer as above.
搜索: 我想为上面的结构制作一个数据结构,它应该能够给出答案的父(问题)以及问题的父(答案)。不要想到解决方案。?
删除和添加: 无需关心。
一个问题可以有多个答案。不,每个问题都必须至少有一个答案。是的,每个问题都有一个问题的答案。
另外,我如何非常有效地进行这种结构编号?
答案 0 :(得分:0)
我会跟进问题:
class Question {
static final String DEFAULT = "DefaultFollowup";
String questionText;
Map<String,Question> responsesAndFollowups;
Question(String text) {
this.questionText = text;
responsesAndFollowups = new HashMap<String,Question>();
}
void addFollowup(String response, Question followup) {
responsesAndFollowups.put(response,followup);
}
void setDefaultFollowup(Question followup) {
addFollowup(DEFAULT,followup);
}
Question getFollowup(String response) {
Question followup = responsesAndFollowups.get(response);
if(followup == null) followup = responsesAndFollowups.get(DEFAULT);
return followup;
}
}
稍后,你可以这样做
Question name = new Question("What is your name?");
Question hobbies = new Question("What is your favorite hobby?");
Question dancing = new Question("What is your favorite dance?");
Question reading = new Question("What is your favorite book?");
name.setDefaultFollowup(hobbies);
hobbies.setDefaultFollowup(reading); // ask people about reading by default
hobbies.addFollowup("Dancing",dancing); // if they say they like dancing, ask that instead
答案 1 :(得分:0)
虽然理论上它是Swing框架的一部分,但JTree类几乎满足了你的所有要求。
答案 2 :(得分:0)
这只是一棵简单的树,所以最基本的实现是:
public class Question {
private String question;
private List<Answer> answers;
//getters setters adding/removing etc
}
public class Answer {
private List<Question> questions;
private String answer;
//getters setters adding/removing etc
}
然后在你的代码中你只需要使用:
List<Question> questionsAsked = new ArrayList<Question>();