-1

ご挨拶、

合格しない単純な条件が 1 つあります。

if(datas.date.getTime()-temps.date.getTime()>=5000)

次から次へと 5 秒経過したかどうかを確認したい。


更新

セッターとゲッターは次のとおりです。

public class Data{
    Date date;
    public Date getDate() {
        return date;
    }
    public void setDate() {
        Date now = new Date();
        this.date = now;
    }
}

だから、私は電話します

Data data;
data.setDate();
processValues(data);

これはプロセス値です:

public void processValues(Data dat){

    if(datas.size()==7){
        writeValues(datas);
        datas=new Vector<Data>();
        temps=new Vector<Data>();
    }
    temps.add(dat);
    datas.add(dat);
}

これは書き込み値です:

public void writeValues(Vector<Data> datas){
    for(i=0;i<temps.size();i++)
        for(j=0;j<datas.size();j++){
            if(temps.elementAt(i).epc==datas.elementAt(j).epc)
                if(datas.elementAt(j).date.getTime()-temps.elementAt(i).date.getTime()>=5000)
                    try {
                        dao.writeToDatabase(datas.elementAt(j));
                        i=j;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
        }

}
4

3 に答える 3

0

You say you have a getter that looks like this:

public Date getDate() {
    return date;
}

Date objects are mutable, so you should probably copy defensively. You can use clone() for this.

Based on that and the syptoms of your problem, perhaps your two Date objects are actually the same object. Have you tried using == (or !=) to confirm that they are indeed separate objects?

Update

Based on the updated information I can see why that condition never passes. I don't really understand what you want you code to do, however. You're essentially just testing if you can create 7 objects in less than 5 seconds, and if you can't then you write some of them out. Either way you clear temps and datas, whether you wrote the objects out or not. My guess is that you do not want to clear these vectors of elements that were not written out. I also don;t understnad why you have both datas and temps when they contain exactly the same elements.

于 2010-12-19T23:44:54.117 に答える
0

processValues()メソッドは次のようになります。

public void processValues(Data dat){
    if(datas.size()==7){
        writeValues(datas);
        datas=new Vector<Data>();
        temps=new Vector<Data>();
    }
    temps.add(dat);
    datas.add(dat);
}

同じインスタンス( "dat")を両方のベクターに追加しています。一方を更新すると、必ずもう一方も更新されるため、dateフィールドに違いが生じることはありません。

于 2010-12-20T00:13:10.643 に答える
0

と を出力datas.date.getTime()した場合temps.date.getTime()、どちらが高いですか?私の推測では、それらが逆になり、減算によって負の数が得られます。もちろん、これは 5000 を超えることはありません。または、データ ポイントが 5 秒離れていません。

于 2010-12-19T23:40:17.843 に答える