Array(java)

时间:2016-02-14 19:29:37

标签: java arrays loops for-loop

从字符串日期notesTimeStampArray数组创建时间戳,并将时间戳(4h,5h,3m,2s等)值存储到我的arrayList数组中,并使用以下代码执行此操作:< / p>

for (int i = 0; i<notesArray.size() && i<notesIDArray.size() ; i++ ){

        try {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);

        Date past = format.parse(notesTimeStampArray.get(i));

        Date now = new Date();

        String TimeStamp;

        if ( TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 60  ) {

        //second
        TimeStamp =  (TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime()) + "s");
        arrayList.add(TimeStamp);
        }

        else if (  TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  >= 60 &&  TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 3600 ) {

        //minute
        TimeStamp =  (TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + "m");
        arrayList.add(TimeStamp);

        } else if ( TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  >= 3600 &&  TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 86400){

        //hour
        TimeStamp =  (TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + "h");
        arrayList.add(TimeStamp);

        } else if ( TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  >= 86400 &&  TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 604800 ){

        //day
        TimeStamp =  (TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + "d");
        arrayList.add(TimeStamp);


        } else if ( TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  >= 604800   ){

        //week
        TimeStamp =  (TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime())/7 + "w") ;
        arrayList.add(TimeStamp);
        }

        }
        catch (Exception j){
        j.printStackTrace();
        }

        data.add(current);
        }
一切都工作得很好  什么时候打印两个数组的值得到:

out notesTimeStampArray

D/Array =: [Sun Feb 14 23:29:26 GMT+05:30 2016, Sun Feb 14 12:12:47 GMT+05:30 2016, Sun Feb 14 12:11:05 GMT+05:30 2016]

out arrayList

D/Array2 =: [1m, 1m, 11h, 1m, 11h, 11h]

你可以看到我的notesTimeStampArray中的项目总数是3,但是arrayList其中6我猜我在循环中做错了,这就是为什么值被多次插入但是我'我试图弄清楚过去2小时真正的问题在哪里,但仍然没有得到任何结果,如果你们有我的错误,那么请指出它对我来说非常有帮助谢谢

更新

我打印了{for循环中的i的值,而out是:

D/i value =: 0
D/i value =: 0
D/i value =: 1
D/i value =: 0
D/i value =: 1
D/i value =: 2

表示i的值如何自动变为0而不是0,1,2,它是0,0,1,0,1,2,

1 个答案:

答案 0 :(得分:1)

首先要帮助您处理代码:

  1. 请尝试至少遵循一些约定(例如,在代码中添加代码)
  2. 在你的if语句和foor循环中,你太过分了。如果第一个语句没有发现该值低于60,则无需检查它是否超过60,因为这已经是给定的事情。
  3. 捕捉异常与没有异常的情况一样好(如果这是你的意图,那就好了)。

    for (int i = 0; i<notesArray.size(); i++ ){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
            Date past = format.parse(notesTimeStampArray.get(i));
            Date now = new Date();
            String TimeStamp;
    
            if ( TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 60  ) {
                //second
                TimeStamp =  (TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime()) + "s");
                arrayList.add(TimeStamp);
            } else if (TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 3600) {
                //minute
                TimeStamp =  (TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + "m");
                arrayList.add(TimeStamp);
            } else if (TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 86400){
                //hour
                TimeStamp =  (TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + "h");
                arrayList.add(TimeStamp);
            } else if (TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 604800){
                //day
                TimeStamp =  (TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + "d");
                arrayList.add(TimeStamp);
            } else {
                //week
                TimeStamp =  (TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime())/7 + "w") ;
                arrayList.add(TimeStamp);
            }
        } catch (Exception j){
            j.printStackTrace();
        }
        data.add(current);
    }
    
  4. 因为我不太了解您的问题,所以我只能给您建议使用IDE的调试模式。尝试在for循环的开头添加一个断点,然后一步一步地执行该程序。