是否可能在java中的一个方法中返回两个ArrayList值? [英] Is it Possible to Return Two ArrayList values in One method in java?
问题描述
有任何方法可以从一个方法返回两个值....
Is there any way to return two values from one method....
示例:
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
我把这个方法调用到我的服务类中,在这里存储 StartDate
和 endDate
列
i'm calling this method into My Service class and in that Storing StartDate
and endDate
into Database here these are Two different Columns
**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
推荐答案
您无法通过一个方法调用分隔结构,但您可以返回组合。例如,返回列表的列表将是一种可能的解决方案:
You cannot return separate structures via one method call, but you can return a composite of them. For example, returning a list of your lists would be a possible solution:
public List<List<Date>> returnTwoArrayList()
{
List<Date> startDate=new ArrayList<Date>();
List<Date> endDate=new ArrayList<Date>();
List<List<Date>> result = new ArrayList<List<Date>>();
result.add(startDate);
result.add(endDate);
return result;
}
您可以使用 get()
方法来检索这些列表。
假设您进行了 List< List< Date>> twoLists.get(0)
可以通过调用
startDate
/ code>和类似 endDate
与 twoLists.get(1)
You can use get()
method to retrieve these lists later on.
Suppose you have made a call like List<List<Date>> twoLists = returnTwoArrayList();
then
you can get startDate
by calling twoLists.get(0)
and similarly endDate
with twoLists.get(1)
这篇关于是否可能在java中的一个方法中返回两个ArrayList值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!