添加到ArrayList时,对象值会更改

时间:2016-03-03 05:34:10

标签: java arraylist time

当尝试创建Timer对象的ArrayList(Timer是一个自定义类)时,在将它们添加到ArrayList之前,所有对象都将更改为最后一个初始化对象

ArrayList<Timer> timerlist1 = new ArrayList<Timer>();
    LocalDateTime current = LocalDateTime.now();
    Timer time1 = new Timer(current,45);
    System.out.println(Timer.getFormattedTime(time1));
    Timer time2 = Timer.getNextTimer(time1);
    System.out.println(Timer.getFormattedTime(time2));
    Timer time3 = Timer.getNextTimer(time2);
    System.out.println(Timer.getFormattedTime(time3));
    Timer time4 = Timer.getNextTimer(time3);
    System.out.println(Timer.getFormattedTime(time4));
    Timer time5 = Timer.getNextTimer(time4);
    System.out.println(Timer.getFormattedTime(time5));

    System.out.println(Timer.getFormattedTime(time1)+"0");
    timerlist1.add(time1);
    System.out.println(Timer.getFormattedTime(time1)+"a");
    timerlist1.add(time2);
    System.out.println(Timer.getFormattedTime(time2));
    timerlist1.add(time3);
    System.out.println(Timer.getFormattedTime(time3));
    timerlist1.add(time4);
    System.out.println(Timer.getFormattedTime(time4));
    timerlist1.add(time5);
    System.out.println(Timer.getFormattedTime(time5));

上面的代码在控制台中打印以下内容

22:24
23:9
23:54
0:39
1:24
1:240
1:24a
1:24
1:24
1:24
1:24

ArrayList应包含

22:24
23:9
23:54
0:39
1:24

但是它包含所有权利的1:24

这是Timer类

  import java.time.*;
import java.util.*;
public class Timer extends Schedule
{
    public  ArrayList<Timer> timers;
    static LocalDateTime start;
    long duration;
    static LocalDateTime end;

    public Timer(){}
    public Timer(LocalDateTime start, long duration)
    {
        this.start = start;
        this.duration = duration;
        this.end = start.plusMinutes(duration);

    }
    public LocalDateTime getStart()
    {
        return start;

    }
    public static Timer getNextTimer(Timer timer)
    {
        start = end;
        Timer i = new Timer(start,45);
        return i;
    }

    public void shortenDuration(long duration)
    {
        long d = duration;
        this.end = start.plusMinutes(d); 
    }
    public LocalDateTime getEndTime()
    {
         return end;
    }
    public void startAt(LocalDateTime end)
    {
        start=end;
    }
    public static String getFormattedTime(Timer timer)
    {
        int hour = start.getHour();
        int minutes = start.getMinute();
        return(String.valueOf(hour)+":"+String.valueOf(minutes));
    }

1 个答案:

答案 0 :(得分:1)

这是因为您的start字段是静态的,因此您创建的所有对象都是相同的。最后一个是1:24。改变开始,因此它不是静态的。

ArrayList<Timer> timerlist1 = new ArrayList<Timer>();
    LocalDateTime current = LocalDateTime.now();
    Timer time1 = new Timer(current,45);
    System.out.println(time1.getFormattedTime(time1));
    Timer time2 = time1.getNextTimer(time1);
    System.out.println(time2.getFormattedTime(time2));
    Timer time3 = time2.getNextTimer(time2);
    System.out.println(time3.getFormattedTime(time3));
    Timer time4 = time3.getNextTimer(time3);
    System.out.println(time4.getFormattedTime(time4));
    Timer time5 = time4.getNextTimer(time4);
    System.out.println(time5.getFormattedTime(time5));

    System.out.println(time1.getFormattedTime(time1)+"0");
    timerlist1.add(time1);
    System.out.println(time1.getFormattedTime(time1)+"a");
    timerlist1.add(time2);
    System.out.println(time2.getFormattedTime(time2));
    timerlist1.add(time3);
    System.out.println(time3.getFormattedTime(time3));
    timerlist1.add(time4);
    System.out.println(time4.getFormattedTime(time4));
    timerlist1.add(time5);
    System.out.println(time5.getFormattedTime(time5));

然后是Timer类

import java.time.*;
import java.util.*;
public class Timer
{
public  ArrayList<Timer> timers;
private LocalDateTime start;
long duration;
private LocalDateTime end;

public Timer(){}
public Timer(LocalDateTime start, long duration)
{
    this.start = start;
    this.duration = duration;
    this.end = start.plusMinutes(duration);

}
public LocalDateTime getStart()
{
    return start;

}
public Timer getNextTimer(Timer timer)
{
    start = end;
    Timer i = new Timer(start,45);
    return i;
}

public void shortenDuration(long duration)
{
    long d = duration;
    this.end = start.plusMinutes(d); 
}
public LocalDateTime getEndTime()
{
     return end;
}
public void startAt(LocalDateTime end)
{
    start=end;
}
public String getFormattedTime(Timer timer)
{
    int hour = start.getHour();
    int minutes = start.getMinute();
    return(String.valueOf(hour)+":"+String.valueOf(minutes));
}
}