Java 以日期为键的多维数组 public void buildArrayForMonth(int-month、int-year、int-numOfDays、JSONArray数组){ JSONObject[]monthArray=null; SimpleDateFormat monthFormat=新的SimpleDateFormat(“M”); SimpleDataFormat yearFormat=新的SimpleDataFormat(“yyyy”); SimpleDataFormat dateFormat=新的SimpleDataFormat(“d”); 对于(int i=0;i
您在初始化之前忘记创建数组(我认为您在示例代码中得到了NullPointerException): 此外,HashMap可能对该任务更有用 UPDOps,还有一个问题,为什么需要二维数组?我认为一维就足够了Java 以日期为键的多维数组 public void buildArrayForMonth(int-month、int-year、int-numOfDays、JSONArray数组){ JSONObject[]monthArray=null; SimpleDateFormat monthFormat=新的SimpleDateFormat(“M”); SimpleDataFormat yearFormat=新的SimpleDataFormat(“yyyy”); SimpleDataFormat dateFormat=新的SimpleDataFormat(“d”); 对于(int i=0;i,java,blackberry,Java,Blackberry,您在初始化之前忘记创建数组(我认为您在示例代码中得到了NullPointerException): 此外,HashMap可能对该任务更有用 UPDOps,还有一个问题,为什么需要二维数组?我认为一维就足够了 monthArray = new JSONObject[32][32]; UPD2我建议使用而不是日期和SimpleDataFormat。这是更正确的方法,例如: JSONObject monthArray = new JSONObject[32]; monthArray[theDate]
monthArray = new JSONObject[32][32];
UPD2我建议使用而不是日期和SimpleDataFormat。这是更正确的方法,例如:
JSONObject monthArray = new JSONObject[32];
monthArray[theDate] = event
UPD3
评论后更新。若一天内可能发生多个事件,那个么您必须使用HashMap和我建议的列表
Calendar c = Calendar.getCalendar();
c.setTimeInMillis(HttpDateParser.parse(date_full));
int theMonth = c.get(Calendar.MONTH);
int theYear = c.get(Calendar.YEAR);
int theDate = c.get(Calendar.DAY_OF_MONTH);
HashMap monthArray=newhashmap();
...
如果(…){
...
列表l=monthArray.get(日期);
if(l==null){
l=新链接列表();
}
l、 添加(事件);
蒙塔雷,普特(西德,l);
}
使用日期作为数组索引有点疯狂。为此,请使用哈希映射
如果你想让一个日期与多个JSONObject关联,那么也许你想使用一个包含列表的映射:HashMap
在一个特定的日期上可以有多个事件。@Adam:如果可以有多个事件,为什么要使用日期作为索引?这听起来很有趣。在这种情况下,你需要o使用HashMap,日期作为键,列表作为值。在Java中,不能像在php中那样动态增加数组长度。@HovercraftFullOfEels可能使用日期作为索引不是一个好主意,原因可能是我们可以同时有两个事件。@HovercraftFullOfEels已修复。我现在手头没有IDE。
Calendar c = Calendar.getCalendar();
c.setTimeInMillis(HttpDateParser.parse(date_full));
int theMonth = c.get(Calendar.MONTH);
int theYear = c.get(Calendar.YEAR);
int theDate = c.get(Calendar.DAY_OF_MONTH);
HashMap<Integer, List<JSONObject>> monthArray = new HashMap<Integer, List>();
...
if (...) {
...
List l = monthArray.get(theDate);
if (l == null) {
l = new LinkedList<JSONObject>();
}
l.add(event);
monthArray.put(theDate, l);
}