所以我想创建一个日历,并希望使用带有日期对象的列表到列表中
lastPartType
但我想知道如何将月份添加到第一个列表中以及将每个月的日期添加到内部列表中。
我尝试list.add()元素但不起作用。
答案 0 :(得分:0)
首先,您需要初始化列表清单
ArrayList<ArrayList<Date>> data = new ArrayList<ArrayList<Date>>();
其次,您需要将列表添加到列表中,如此
data.add(new ArrayList<Date>());
然后,您可以将Date对象添加到索引0处的特定列表,如此
data.get(0).add(new Date());
12个列表的示例设置,每月一个
for(int i = 0; i < 12; i++)
{
data.add(new ArrayList<Date>());
}
data.get(0) //is the list for January
data.get(5) //the list for June
data.get(11) //the list for December
要将一天添加到六月,您可以执行以下操作
data.get(5).add(new Date()); //adds a date to the list at index 5 (June)
答案 1 :(得分:0)
所以你绝对可以通过RAZ_Muh_Taz建议的方法做你想做的事。不过你确定那是你想要的吗?根据事情的声音,你可以更好地使用不同的数据结构,比如地图:
Map map = new HashMap();
map.put("January", new list<date>());
map.get("January"); # = your date
对我而言,这更符合您的目标:)。