如何将字符串列表转换为arraylist并单独调用它?

时间:2013-05-31 05:03:00

标签: java date arraylist while-loop

我有一个字符串列表来自循环语句,我不知道如何使它成为一个ArrayList然后单独调用字符串。这是while语句:

try {
    toDate=format.parse(str4);
    java.util.Date newValue= new SimpleDateFormat(OLD_FORMAT).parse(str4);
    String newValue2 = new SimpleDateFormat(NEW_FORMAT).format(newValue);
    // System.out.println(newValue2);
} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}


//change the format of toDate 
try {
    newDateString=format.parse(str5);
    java.util.Date newqwe = new SimpleDateFormat(OLD_FORMAT).parse(str5);
    String newqwe2=new SimpleDateFormat (NEW_FORMAT).format(newqwe);
    //System.out.println(newqwe2);
} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

//get the days between two dates then print it 
Calendar cal2 = Calendar.getInstance();
cal2.setTime(toDate);
while (cal2.getTime().before(newDateString)) {
    cal2.add(Calendar.DATE, 1);
    String datelist=(format.format(cal2.getTime()));

str4来自一个与str5相同的标签,当点击一个按钮时,循环语句中的字符串将是这样的:

2013-05-03
2013-05-04
2013-05-05
2013-05-05
2013-05-06
2013-05-07
2013-05-08
2013-05-09
2013-05-10
2013-05-11
2013-05-12
2013-05-13
2013-05-14
2013-05-15
2013-05-16
2013-05-17

1 个答案:

答案 0 :(得分:2)

List<String> list = new ArrayList<String>();

 list.add("2013-05-03");
 list.add("2013-05-04");
 ........etc

 for(String str:list)
  System.out.prinltn(str); // prints all the strings in list


System.out.println(list.get(index)); // prints string at specified index