gpt4 book ai didi

java - 如何创建时间数组

转载 作者:行者123 更新时间:2023-12-01 15:48:39 27 4
gpt4 key购买 nike

我有一个数组列表 arr,其中包含一系列数字,例如 07, 52, 25, 10, 19, 55, 15, 18, 41。在此列表中,第一项是小时,第二项是分钟,第三项是第二项,比如 07:52:25。现在我想创建一个时间数组,在其中插入这些值并进行一些算术运算,例如第一个索引和第二个索引之间的差,这给了我时间差。那么我该怎么做呢?

ArrayList arr = new ArrayList();
StringTokenizer st = new StringTokenizer(line, ":Mode set - Out of Service In Service");
while(st.hasMoreTokens()){
arr.add(st.nextToken());
}

最佳答案

好吧,我想我明白你想要你的代码做什么。这是我的做法。

public class DateHandler
{
public DateHandler(int seconds, int minutes, int hours)
{
this.seconds = seconds;
this.minutes = minutes;
this.hours = hours;
}

public String toString()
{
return "Seconds: "+seconds+" Minutes: "+minutes+" Hours: "+hours;
}

public int seconds;
public int minutes;
public int hours;
}

public class Main
{
public static void main(String args[])
{
int[] data = {07, 52, 25, 10, 19, 55, 15, 18, 41}
int numberOfDates = data.length/3//Divide by 3 because there are 3 numbers per date
ArrayList<DateHandler> dates = new ArrayList<DateHandler>(numberOfDates);
for(int x=0;x<numberOfDates;x++)
{
int index = x*3;
DateHandler date = new DateHandler(data[index],data[index+1],data[index+2]);
System.out.println("added date: "+date.toString());
dates.add(date);
}

//here you can do your calculations.
}
}

我希望这有帮助!

关于java - 如何创建时间数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6579719/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com