2015-12-14 5 views
6

私はトラブルの要素を見つけるのを持っているを見つけ、ここに私のコードは次のとおりです。ArrayListにJavaで要素

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はしてオブジェクトを取り戻すENを検索する方法がわかりません2015-03-10。 ところでこれはCVSのデータの1つのラインである:20、GPS/GpsAccuracyGyroBias; 0; 0; 0:2015年3月10日7; 20:13

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

14100072 2015年3月10日07:12

//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); 
+1

を 'getEndTime'も、文字列をバック得ていますか? – npinti

+0

x.getEndTime()は何を返しますか?それは戻って、ちょうど日付か「2015-03-10 7:13:20」のような時間とともに?それが時間とともに返された場合、 'if(x.getEndTime()。equals(findThis)){'は合格しません。 – Parasu

+0

あなたは 'EndTimeComparator'のコードを表示できますか? – Parasu

答えて

2

getEndTime()がLocalDateTimeを返すと推測すると、文字列をLocalDateTimeの型と比較することはできません。あなたは、LocalDateにLocalDateTimeを解析し、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()); 

    } 
} 
+0

ありがとう、これは完璧に機能しました。これで私は日付だけを調整することができ、それは与えられた日付のオブジェクトを私に与えるでしょう。 – H35am

0

あなたはそのヌルまたはMonitoringのためのコンパレータを提供する必要がComparableを実装しなければならない(それらの両方が、そのあなたの時間フィールドによって項目を比較する必要があります必要)。

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

は例のデータによると、あなたは

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

はそう動作しません equalsを使用して、ではない "2015-03-10" "2015-03-10 7:13:20"提供されます。代わりに、あなたは 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オブジェクトまたは同様に変換します。

関連する問題