[英]What data structure to use?
I need to represent the below data (in Java): 我需要表示以下数据(在Java中):
I was thinking of using a TreeMap, but not sure how. 我在考虑使用TreeMap,但不知道如何使用。 Any ideas?
有任何想法吗?
Considerations, assuming you're interested in managing calendar entries : 考虑因素,假设您对管理日历条目感兴趣 :
THE MODEL 该模型
// best to use ENUM for fixed set of constants
enum Month {
JANUARY, FEBRUARY, ... , NOVEMBER, DECEMBER
}
enum Weekday {
SUNDAY, MONDAY, ... , FRIDAY, SATURDAY
}
/**
* The day "data node". Fill in constructors/accessors.
*/
class Day {
int year;
Month month;
Weekday weekday;
String date; // hashkey
String entry; // the entry
}
/**
* The calendar, simply mapping a unique date to it's day.
* Create a date like: year + "-" + MONTH + "-" + DAY
*/
HashMap<String, Day> calendar;
THE VIEW 风景
Since our data structure is not sparse, an independent view would have to simulate a full calendar. 由于我们的数据结构不稀疏,因此独立视图必须模拟完整日历。 Display all days/generate all dates according to the rules of calendars, but only add a day to the
HashMap
if a new entry is saved. 根据日历规则显示所有日期/生成所有日期,但如果保存新条目,则仅向
HashMap
添加一天。
NOTES 笔记
HashMap
in a class to arbitrate CRUD operations on days
. HashMap
包装在一个类中,以便在days
对CRUD操作进行仲裁。 month
, or delete a year
, consider having a three-level map like year => month => day
alongside above. month
所有日期,或者删除year
,请考虑使用三年级地图,例如year => month => day
以上。 JTree from Swing can also be used as data structure. 来自Swing的JTree也可以用作数据结构。
But the first question you should ask yourself is "How I want to access the data". 但你应该问自己的第一个问题是“我想如何访问数据”。
You would need some sort of tree structure. 你需要某种树形结构。 This might be your starting point:
这可能是你的出发点:
public class Node {
private String label;
private List<Node> children;
//add Constructors, getters, setters, and member methods, etc
}
Node root = new Node();
root.setLabel("2012");
//children of root are "01", "02", etc.
Definitely separate model data from view data. 绝对独立于视图数据的模型数据。
Here is a different model than the one proposed in the paislee answer. 这是与paislee答案中提出的模型不同的模型。
Map<Calendar, String> thisIsAllYouNeedForTheModel = new HashMap<Calendar, String>();
Calendar thisIsTheKey = Calendar.getInstance();
thisIsTheKey.clear();
thisIsTheKey.set(Calendar.YEAR, theYear);
thisIsTheKey.set(Calendar.MONTH, theMonth);
thisIsTheKey.set(Calendar.DAY_OF_MONTH, theMonth);
thisIsTheKey.set(Calendar.HOUR, theHour);
thisIsTheKey.set(Calendar.MINUTE, theMinute);
thisIsTheKey.set(Calendar.SECOND, theSecond);
thisIsAllYouNeedForTheModel.put(thisIsTheKey, data);
Edit: silly me. 编辑:傻我。
Map<Calendar, String>
is my suggestion. Map<Calendar, String>
是我的建议。
I would suggest the TreeMap, but if you want to make experiments, just go ahead and use LinkedList. 我建议使用TreeMap,但是如果你想进行实验,请继续使用LinkedList。 Acessing the data is pretty complex if you iterate through many lists.
如果遍历许多列表,则访问数据非常复杂。 But it was a lot of fun experimenting.
但实验起来很有趣。
EDIT: Here is a tutorial including a package that allows you to use a TreeMap or something similiar to a Tree: http://code.google.com/p/qed-java/wiki/HowToUseTree 编辑:这是一个教程,包括一个允许您使用TreeMap或类似树的东西的软件包: http : //code.google.com/p/qed-java/wiki/HowToUseTree
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.