[英]Check times within an Array and Print the latest in a Time Range
Let's say I have a list of data in the following format:假设我有以下格式的数据列表:
ID#,YYYYMMDD HH:MM:SS.sss,score
So the data could look like this:所以数据可能是这样的:
501,20220104 13:12:07.005,25
501,20220104 13:12:07.002,25
500,20220106 09:04:10.013,10
501,20220104 13:12:07.001,25
501,20220104 13:12:07.003,25
501,20220104 13:12:07.004,25
501,20220104 15:20:50.011,25
I want to program the following in Java :我想在Java中编写以下程序:
20220104 13:12:07.001
).20220104 13:12:07.001
) 的最早日期时间和 Arcade ID。 I think I already know how to split up the data:我想我已经知道如何拆分数据了:
ArrayList<String> scoreData = new ArrayList<>(Arrays.asList(data.split(",")));
String arcadeID = scoreData.get(0);
Date dateTime = scoreData.get(1);
int score = scoreData.get(2);
And then how to pass that data to a new Array and sort it afterwards in descending order:然后如何将该数据传递给一个新数组,然后按降序对它进行排序:
Date[] scoresAbove20;
if (score > 20) {
scoresAbove20.push(dateTime);
Arrays.sort(scoresAbove20);
}
From the scoresAbove20
Array, I am not sure how to:从
scoresAbove20
数组中,我不确定如何:
Define a class, a record specifically, to represent each row of data.定义一个class,具体是一条记录,代表每一行数据。
record Score( int id , LocalDateTime when , int score ) { }
Never use either of the legacy Date
classes.切勿使用任何旧的
Date
类。 For date-time handling, use only the modern java.time classes defined in JSR 310.对于日期时间处理,仅使用 JSR 310 中定义的现代java.time类。
Define a formatter to parse your date-time string.定义一个格式化程序来解析您的日期时间字符串。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "uuuuMMdd HH:mm:ss.SSS" );
Parse as a LocalDateTime
object, a date with time-of-day but lacking the context of a time zone or offset-from-UTC.解析为
LocalDateTime
object,一个带有时间但缺少时区上下文或与 UTC 的偏移量的日期。
After parsing each of the three input fields, instantiate a record.在解析了三个输入字段中的每一个之后,实例化一条记录。
Score s = new Score( id , when , score );
Add that object to a list.将该 object 添加到列表中。
List < Score > scores = new ArrayList <>( lines.size() );
When done, you should have a list like this one:完成后,您应该有一个像这样的列表:
scores.toString() = [Score[id=501, when=2022-01-04T13:12:07.005, score=25], Score[id=501, when=2022-01-04T13:12:07.002, score=25], Score[id=500, when=2022-01-06T09:04:10.013, score=10], Score[id=501, when=2022-01-04T13:12:07.001, score=25], Score[id=501, when=2022-01-04T13:12:07.003, score=25], Score[id=501, when=2022-01-04T13:12:07.004, score=25], Score[id=501, when=2022-01-04T15:20:50.011, score=25]]
scores.toString() = [Score[id=501, when=2022-01-04T13:12:07.005, score=25], Score[id=501, when=2022-01-04T13:12:07.002, score= 25], 分数[id=500, when=2022-01-06T09:04:10.013, score=10], 分数[id=501, when=2022-01-04T13:12:07.001, score=25], 分数[id=501, when=2022-01-04T13:12:07.003, score=25], 得分[id=501, when=2022-01-04T13:12:07.004, score=25], 得分[id=501 , when=2022-01-04T15:20:50.011, score=25]]
String input =
"""
501,20220104 13:12:07.005,25
501,20220104 13:12:07.002,25
500,20220106 09:04:10.013,10
501,20220104 13:12:07.001,25
501,20220104 13:12:07.003,25
501,20220104 13:12:07.004,25
501,20220104 15:20:50.011,25
""";
List < String > lines = input.lines().toList();
record Score( int id , LocalDateTime when , int score ) { }
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "uuuuMMdd HH:mm:ss.SSS" );
List < Score > scores = new ArrayList <>( lines.size() );
for ( String line : lines )
{
String[] fields = line.split( "," );
if ( fields.length != 3 ) { throw new IllegalStateException( "Did not find three fields." ); }
int id = Integer.parseInt( fields[ 0 ] );
LocalDateTime when = LocalDateTime.parse( fields[ 1 ] , formatter );
int score = Integer.parseInt( fields[ 2 ] );
Score s = new Score( id , when , score );
scores.add( s );
}
System.out.println( "scores = " + scores );
Now you have some objects on which you can apply your business logic, as stated in your 1-2-3 items.现在您有了一些可以应用业务逻辑的对象,如 1-2-3 项中所述。
I'll not do your entire schoolwork assignment for you, and I have probably done too much.我不会为你完成所有的作业,而且我可能做的太多了。 So I will leave you with this key point for performing that logic: To check for which items are in a two minute period, Use the
LocalDateTime
methods isEqual
, isBefore
, isAfter
, to compare.因此,我将为您提供执行该逻辑的关键点:要检查两分钟内有哪些项目,请使用
LocalDateTime
方法isEqual
、 isBefore
、 isAfter
进行比较。
I recommend using the Half-Open approach to defining a span of time.我建议使用半开放方法来定义时间跨度。 This means the beginning is inclusive while the ending is exclusive .
这意味着开始是包含的,而结束是排他的。 So you will be checking if the score's date-time is equal to or after the beginning and the score's date-time is before the end.
因此,您将检查乐谱的日期时间是否等于或晚于开始以及乐谱的日期时间是否早于结束。 Tip: A shorter way of asking "is equal to or after" is to ask "is not before".
提示:询问“等于或之后”的更简短方式是询问“不在之前”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.