How i can store the following in some efficient data structure [duplicate]
Possible Duplicate:
Need help in structuring things in XML file
have a structure like follows:
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.
Searches: I want to make a datastructure for the above structure which should be able to give me the parent(question) of an answer and also the parent(answer) of a question.can't think of a solution..?
Deletion and addition: No need to care about.
开发者_如何学CA question can have multiple answers. No, every question must have at least one answer. Yes, every question decending from an answer.
Also how I can do this structure numbering very efficiently?
I would have followup questions:
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;
}
}
Later, you can do this
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
Though it is theoretically a part of the Swing framework, the JTree class meets pretty much all your requirements.
Well this is just a simple tree so the very basic implementation would be:
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
}
Then in your code you just need to use a:
List<Question> questionsAsked = new ArrayList<Question>();
精彩评论