2012-12-14 54 views
4

有沒有辦法從一個方法返回兩個值....是否有可能在Java中的一種方法返回兩個ArrayList值?

例子:

public Class Sample 
{ 
    public List<Date> returnTwoArrayList() 
    { 
     List<Date> startDate=new ArrayList<Date>(); 

     List<Date> endDate=new ArrayList<Date>(); 

    //So I want to Return Two values startDate and endDate ...It is Possible???? 
    } 
} 

Edit:1 

我打電話此方法爲我服務類,並在這裏存放StartDateendDate到數據庫這些是兩個不同的列

**StartDate  endDate** 
2012-12-01  2012-12-05 
2012-12-01  2012-12-15 
2012-12-02  2012-12-10 
2012-12-20  2012-12-25 
2012-12-25  2012-12-31 

回答

13

不能通過一個方法調用返回單獨的結構,但你可以返回一個複合他們。例如,回到你的列表清單將是一個可能的解決方案:

public List<List<Date>> returnTwoArrayList() 
    { 
     List<Date> startDates = new ArrayList<Date>(); 
     List<Date> endDates = new ArrayList<Date>(); 

     List<List<Date>> result = new ArrayList<List<Date>>(); 
     result.add(startDates); 
     result.add(endDates); 

     return result; 
    } 

您可以使用get()方法稍後檢索這些名單。 假設你已經像List<List<Date>> twoLists = returnTwoArrayList();一個電話,然後 您可以通過調用twoLists.get(0)twoLists.get(1)

+0

好的...然後我想分開StartDate和endDate在另一個服務類當時如何做? –

+0

@NarasimhamK看到我的答案,我補充說明。 – Juvanis

+0

謝謝...我正在測試.. –

6

不,你不能從方法返回兩個值。

最好的方法是創建一個帶有兩個字段的自定義類並返回該對象。

class ObjectHolder{ 
    private List<Date> startDate=new ArrayList<Date>(); 
    private List<Date> endDate=new ArrayList<Date>(); 

    <getter & setter method> 
} 

和 -

public ObjectHolder returnTwoArrayList(){ 
    ObjectHolder oh = new ObjectHolder(); 
    List<Date> startDate=new ArrayList<Date>(); 
    oh.setStartDate(startDate); 
    List<Date> endDate=new ArrayList<Date>(); 
    oh.setEndDate(endDate); 
    return oh; 
} 
+0

那該怎麼辦通過從它那裏得到這兩個值... –

+0

getters :-) – Subin

+0

正確..它可以創建Bean類但是是他們的任何其他替代方式,我的意思是隻在類中....感謝您的重播.. –

1

您可以創建一個自定義類這樣

TimeSpan(ArrayList<Date> startDate, ArrayList<Date> endDate) 

,並返回該對象。

1

直接,不,除非你返回兩個List秒的陣列,或者用規定的理解給調用者,這將正好包含兩個條目和什麼樣的價值每一個將代表ListList A S。

除此之外,您總是可以編寫一個Pair類並使用它,這樣會更好,因爲它消除了對該返回值維度的假設的依賴性,並且您會發現Pair類型派上用場在許多情況下。

2

得到startDate,同樣endDate可以

  • 提供一個或兩個列表作爲參數(S)來填充。
  • 有兩個列表,它們是方法實例的字段並通過getter訪問它們。
  • 返回兩個列表或列表的列表。
  • 返回包裝這兩個列表的自定義類型。我不會使用getter,只是公開這些字段。
  • 有間隔

的一個列表,我相信最後是最好的解決方案。

public class Sample { 
    public List<Interval> returnListOfStartToEndDates() { 
     List<Interval> intervals=new ArrayList<>(); 

     return intervals; 
    } 
} 

喬達時有Interval類,這是比創建兩個Date對象更有效,但你也可以創建自己的。

1

相反,你可以使用地圖類似下面,

public Map<String, List<Date>> getBothDates() { 
    List<Date> startDate = new ArrayList<Date>(); 
    List<Date> endDate = new ArrayList<Date>(); 
    Map<String, List<Date>> bothDates = new HashMap<String, List<Date>>(); 
    bothDates.put("startDate", startDate); 
    bothDates.put("endDate", endDate);  
    return bothDates; 
} 

要取兩個日期簡單迭代地圖,

public void printBothDates() { 
    Map<String, List<Date>> bothDates = getBothDates(); 
    for(Entry<String, List<Date>> entry : bothDates.entrySet()) { 
    if(StringUtils.equalsIgnoreCase("startDate", entry.getKey())) { 
     for(Date d : entry.getValue()) { 
      System.out.println("startDate---"+d); 
     } 
    } else if(StringUtils.equalsIgnoreCase("endDate", entry.getKey())) { 
     for(Date d : entry.getValue()) { 
      System.out.println("endDate---"+d); 
     } 
    } 
    } 
} 
相關問題