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();
}
}
}
私は基本的にthedateがJSONObjectを含む配列であることを望んでいます。私のアプリは私が今持っているものでクラッシュします。あなたがこれを行うことができるかどうかはわかりません。 Javaにはpushやaddのようなものがありますか?あなたは初期化する前に配列を作成するのを忘れその日をキーとする多次元配列
特定の日付に複数のイベントが存在する場合があります。 – Adam
@Adam:複数のイベントが存在する可能性がある場合、なぜDateをインデックスとして使用したいのですか?これは面白いにおいがし始めています。 –
その場合、HashMapを日付としてキーとListを値として使用する必要があります。 Javaでは、PHPのように配列の長さを動的に増やすことはできません。 – 4ndrew