Java 如何在一些有效的数据结构中存储以下内容
可能重复:Java 如何在一些有效的数据结构中存储以下内容,java,data-structures,Java,Data Structures,可能重复: 具有如下结构: 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 Question
- 具有如下结构:
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.
还有,我如何才能非常有效地进行结构编号?我会有以下问题:
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;
}
}
虽然理论上它是Swing框架的一部分,但该类几乎满足您的所有需求。这只是一个简单的树,因此最基本的实现是:
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
}
公开课问题{
私有字符串问题;
私人名单答案;
//getters setters添加/删除等
}
公开课答案{
私人名单问题;
私有字符串应答;
//getters setters添加/删除等
}
然后在代码中,您只需要使用:
List<Question> questionsAsked = new ArrayList<Question>();
List questionsAsked=new ArrayList();
这似乎是您提交的副本。您应该创建一个涵盖XML和Java的通用数据模型!那是关于XML…这是关于java数据结构…完全不同…可能是相同的结构字符串误导了你…很抱歉…有很多问题没有回答:数据是相当静态的还是经常更新/删除?可以删除答案/问题吗?您希望执行哪些类型的搜索(如果有)?要选择正确的数据结构,您需要了解您的需求。@peter:我更新了我的问题。:)@saurabh ranu:我打赌这没有什么不同,您应该创建一个逻辑数据(对象)模型,然后根据您的需求派生出具体的实现(XML、java、REST等)。我的意见:-)对不起,我没有使用swing。。。。我使用的是struts…您不必使用Swing。只需使用JTree类和TreeNode。这可能吗??我可以从swing软件包中提取这两个类吗?您不打算提取它们。。。只需添加javax.swing.JTree、javax.swing.JTree和javax.swing.tree.TreeNode的导入,并在代码中使用它们。您可能想在这里阅读JTree包文档:一个问题可以有多个答案……我的意思是,一个问题只能有孩子的答案,可以有尽可能多的答案,答案可以有尽可能多的问题,等等。唯一的限制是,问题不应将问题作为直接子项,而答案不能将答案作为直接子项。如果glowcoder对响应和后续操作使用多重映射而不是哈希映射,这种方法就足够了。现在一个答案可能有多个后续问题。您可以在Google Guava library中找到一个很好的多重映射实现。@Zenzen如果您能够从中进行选择,那就太好了。如果你不想有一个外部依赖关系,用一个列表代替问题就足够了。是的,当然列表就足够了。我的意思是多重映射就是一个可以将多个(即列表/集合/数组)值分配给单个键的映射!看起来很有希望。。你能给我看一些演示吗。。。。
List<Question> questionsAsked = new ArrayList<Question>();