4

有没有办法从一种方法返回两个值....

例子:

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????
   }
}

我将此方法调用到我的服务类中,并且在该存储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
 
4

7 回答 7

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();那么你可以startDate通过调用来获得twoLists.get(0) ,类似endDatetwoLists.get(1)

于 2012-12-14T12:02:59.600 回答
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;
}
于 2012-12-14T11:59:22.580 回答
2

你可以

  • 提供一个或两个列表作为要填充的参数。
  • 有两个列表,它们是方法实例的字段,并通过 getter 访问它们。
  • 返回两个列表的数组或列表的列表。
  • 返回一个包含两个列表的自定义类型。我不会使用吸气剂,只是将字段公开。
  • 有一个间隔列表

我相信最后一个是最好的解决方案。

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

      return intervals;
   }
}

Joda-time 有一个Interval类,它比创建两个 Date 对象更有效,但您也可以创建自己的。

于 2012-12-14T12:09:54.003 回答
1

你可以像这样创建一个自定义类

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

并返回该对象。

于 2012-12-14T12:01:53.727 回答
1

List直接,不,除非您计算返回一个由两个s 或一个s组成的数组,List并且List向调用者明确表示它将包含两个条目以及每个条目将代表什么值。

除此之外,你总是可以编写一个Pair类并使用它,这会更好,因为它消除了对关于返回值维度的假设的依赖,你会发现一个Pair类型在很多很多情况下都很方便。

于 2012-12-14T12:02:24.570 回答
1

相反,您可以使用 Map 如下,

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);
       }
     }
   }
}
于 2014-06-30T11:42:29.007 回答
0
import java.util.ArrayList;

public class MyClass {
    public ArrayList<ArrayList> TwoArrayList() {

        ArrayList cars = new ArrayList();
        cars.add("Volvo");
        cars.add("BMW");
        cars.add("Ford");
        cars.add("Mazda");

        ArrayList fruits = new ArrayList();
        fruits.add("Apple");
        fruits.add("Mango");
        fruits.add("Banana");
        fruits.add("Orange");

        ArrayList <ArrayList> twoArrayList = new ArrayList <ArrayList>();
        twoArrayList.add(cars);
        twoArrayList.add(fruits);

        return twoArrayList;

    }

    public static void main(String[] args) {
       MyClass obj = new MyClass();

        ArrayList <ArrayList> Arlist= obj.TwoArrayList();
        
        System.out.println(Arlist.get(0));/**FIRST ARRAY LIST*/
        
        System.out.println(Arlist.get(0).get(0));/**FIRST ARRAY LIST FIRST Element*/

        System.out.println(Arlist.get(1));/**SECOND ARRAY LIST*/
        
        System.out.println(Arlist.get(1).get(0));/**SECOND ARRAY LIST FIRST Element*/
    }  
}  

输出在这里

在此处输入图像描述

于 2020-10-13T02:23:20.123 回答