【问题标题】:Find the Value in List [closed]在列表中查找值 [关闭]
【发布时间】:2012-12-02 11:01:44
【问题描述】:

如何在列表中找到特定值.. 前任: 这是在 Demo1 类方法中

   /*some code*/ ---------
    ---------//Generating List<Date> d1 and d2 hear  
    public List<Date> returnList(List<Date> d1,List<Date> d2)
    {
          List<Date> startDate=new ArrayList<Date>();

          startDate.add(d1);
          startDate.add(d2);
         return startDate;
    }

我在 Demo2 类中使用这个方法在服务类中我想要相同的 d1,d2 列表。

【问题讨论】:

  • 我无法解析这个问题。
  • @PradeepSimha:谁说列表是排序好的?
  • 使用整数解析列表,然后使用:list.get(integer);这样就可以了。
  • 以上无法编译。那是问题吗?你想让我们解释一下你从编译器得到的错误信息是什么意思吗?你读过吗?

标签: java collections return return-value


【解决方案1】:

我想你想要List#indexOf:

int x = listOfDates.indexOf(theDate);

返回元素的(第一个)索引,如果不在列表中,则返回 -1。

取决于equals() 能否正常工作,这对日期可能会或可能不会很棘手(您必须设置完全相同的时间)。

【讨论】:

    【解决方案2】:

    我不喜欢它,但要编译它,你需要类似

     public List<List<Date>> returnList(List<Date> d1,List<Date> d2)
    {
          List<List<Date>> startDate=new ArrayList<List<Date>>();
    
          startDate.add(d1);
          startDate.add(d2);
         return startDate;
    }
    

    您的返回类型应该是列表列表

    【讨论】:

      猜你喜欢
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      相关资源
      最近更新 更多