如何从ArrayList逐个返回

时间:2012-10-16 00:58:29

标签: java arraylist

我有arraylist说,

List<CalendarOutput> RecuringEve= Recurrent.eventView(component,begin,end);

我想打印arraylist,

CalendarOutput caldavOutput = ListUtil.getReComponent(component, RecuringEve);

这里有方法getReComponent(component,RecuringEve)

public CalendarOutput getReComponent(Component component, List recuringEve) {
for(int i = 0; i < recuringEve.size(); i++)   {  
  CalendarOutput.DTSTART_FULL=recuringEve.get(i).toString();
 }
retrun CalendarOutput;
}

此方法必须逐个返回值。但它只返回Arraylist的最后一个值。如何逐一返回值

3 个答案:

答案 0 :(得分:3)

方法仅返回一次。您无法多次返回,但您可以多次调用方法。如果您想要返回多个值,只需在列表/集合中收集这些值,然后返回列表/集合

答案 1 :(得分:2)

你不必做一个方法。你可以使用For each来打印arraylist。

import java.util.ArrayList;

public class MainClass {
  public static void main(String args[]) {
    List<CalendarOutput> RecuringEve= Recurrent.eventView(component,begin,end);
    CalendarOutput caldavOutput = ListUtil.getReComponent(component, RecuringEve);

    System.out.print("Original contents of vals: ");
    for (int v : caldavOutput)
      System.out.print(v + " ");
  }
}

Credit java2s

答案 2 :(得分:0)

public CalendarOutput getReComponent(Component component, List recuringEve) {
for(int i = 0; i < recuringEve.size(); i++)   {
  // Do you just want to append? then use +=
  CalendarOutput.DTSTART_FULL+=recuringEve.get(i).toString();
 }
retrun CalendarOutput;
}