【问题标题】:Find element in ArrayList Java在 ArrayList Java 中查找元素
【发布时间】:2016-03-19 16:22:10
【问题描述】:

我找不到元素,这是我的代码:

 public static void main(String[] args) {
    BufferedReader br = getFileReader("reader.csv");

    ArrayList<Monitoring> col = getCollection(br);

    //sort the collection on 'beginTime'
    for (Monitoring x : col)
        System.out.println(x.toString());
    BeginTimeComparator beginTime = new BeginTimeComparator();
    Collections.sort(col,beginTime);
    System.out.println("Begin time:");
    for (Monitoring x : col)
        System.out.println(x.toString());

这是我遇到问题的部分,我不知道如何搜索以获取 endTime 2015-03-10 的对象。 顺便说一句,这是一行 cvs 数据:

UnitId;BeginTime;EndTime;Type;Min;Max;Sum

14100072;2015-03-10 07:12:20;2015-03-10 7:13:20;Gps/GpsAccuracyGyroBias;0;0;0

//find the amount of elements that were sent on 'endTime' = 2015-03-10 (just the date)
    EndTimeComparator endTime = new EndTimeComparator();
    String findThis = "2015-03-10";
    Collections.sort(col, endTime);

    for(Monitoring x : col){
             if(x.getEndTime().equals(findThis)){
                 System.out.println("Here is 'endTime= 2015-03-10' :");
                 System.out.println(x.toString());

             }
    }

我试过了,但都没有用:

int index = Collections.binarySearch(col, findThis.toString(), null);
System.out.println("Here is 'endTime= 2015-03-10' :");
System.out.println(index);

【问题讨论】:

  • getEndTime 是否也返回一个字符串?
  • x.getEndTime() 返回什么?它会返回,只是日期还是像 '2015-03-10 7:13:20' 这样的时间?如果随时间返回,则条件if(x.getEndTime().equals(findThis)){不会通过。
  • 你能出示EndTimeComparator的代码吗?
  • 再观察:结束时间显示为2015-03-10 7:13:20而不是2015-03-10 07:13:20,即07而不是7。如果字符串的长度是固定的,那么你可以使用compareTo方法查找哪个时间小于或大于或等于的字符串。注意:如果日期和时间的格式为 yyyy-MM-dd HH:mm:ss,那么它可以正常工作。
  • 另一方面,在 ArrayList 中搜索可能非常无效。如果您正在寻找一个 endTime,那么您可以用于优化的任何东西实际上都会更加昂贵。但是请确保您知道哪些操作在哪个集合中成本高昂,如果您有一个包含数千或数百万条记录的集合,那么无效的解决方案可能会造成严重后果。

标签: java arraylist collections


【解决方案1】:

猜测 getEndTime() 返回的是 LocalDateTime,您无法将字符串与 LocalDateTime 类型进行比较。您可以尝试将 LocalDateTime 解析为 LocalDate 并使用 LocalDate 类型填充“findThis”变量。

因为代码说了超过 1000 个单词:

EndTimeComparator endTime = new EndTimeComparator();
Collections.sort(col, endTime);

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate findThis = LocalDate.parse("2015-03-10", dtf);

System.out.println("Here is 'endTime= 2015-03-10' :");
for (Monitoring x : col) {
    if (x.getEndTime().toLocalDate().equals(findThis)) {

        System.out.println(x.toString());

    }
}

【讨论】:

  • 谢谢,这很完美。有了这个我只能调整日期,它会给我给定日期的对象。
【解决方案2】:

您需要为该 null 提供 Comparator 或 Monitoring 应该实现可比较(它们都应该按您需要的时间字段比较项目)。

Collections.binarySearch(col, findThis.toString(), null);

【讨论】:

    【解决方案3】:

    根据您提供的示例数据

    UnitId;BeginTime;EndTime;Type;Min;Max;Sum
    14100072;2015-03-10 07:12:20;2015-03-10 7:13:20;Gps/GpsAccuracyGyroBias;0;0;0
    

    endTime"2015-03-10 7:13:20",而不是"2015-03-10",所以使用equals 将不起作用。相反,您可以尝试使用startsWith:

    String findThis = "2015-03-10";
    for (Monitoring x : col) {
        if (x.getEndTime().startsWith(findThis)) {
            System.out.println("Here is 'endTime= 2015-03-10': ");
            System.out.println(x.toString());
        }
    }
    

    甚至更好:当您从 CSV 读取对象时,不要将开始时间和结束时间存储为字符串,而是将它们转换为 Date 对象或类似对象。

    【讨论】:

      猜你喜欢
      • 2013-05-07
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 2015-01-11
      • 2021-03-19
      • 1970-01-01
      相关资源
      最近更新 更多