public void buildArrayForMonth(int month, int year, int numOfDays, JSONArray array){
JSONObject[][] monthArray = null;
SimpleDateFormat monthFormat = new SimpleDateFormat("M");
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat dateFormat = new SimpleDateFormat("d");
for(int i=0;i<array.length();i++){
try {
JSONObject event = array.getJSONObject(i);
String date_full = event.getString("date_full");
Date date = new Date(HttpDateParser.parse(date_full));
int theMonth = Integer.parseInt(monthFormat.format(date)) - 1;
int theYear = Integer.parseInt(yearFormat.format(date));
int theDate = Integer.parseInt(dateFormat.format(date));
if(theMonth == month && theYear == year){
System.out.println("This Month" + date_full);
monthArray[theDate][monthArray[theDate].length] = event; //add event object to the month array and its respective date
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我基本上希望date是一個包含JSONObjects的數組。我的應用程序崩潰,現在我擁有了。我不確定你是否能夠做到這一點。 Java有沒有像推或添加?以天爲鍵的多維數組
有可能在某一特定日期多個事件。 – Adam 2012-01-07 20:57:44
@Adam:如果可以有多個事件,那麼爲什麼要使用Date作爲索引?這開始聞起來很有趣。 – 2012-01-07 21:03:37
在這種情況下,您需要使用HashMap,其中日期作爲鍵和List作爲值。在Java中,你不能動態增加數組的長度,例如在php中。 – 4ndrew 2012-01-07 21:04:26