简体   繁体   English

检查数组中的时间并打印时间范围内的最新时间

[英]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中编写以下程序:

  1. Submit data to an Array only if the Score is above 20 (in this example list, 6 out of 7 are).仅当分数高于 20 时才将数据提交到数组(在此示例列表中,7 个中有 6 个是)。
  2. Order the submitted list in descending order from earliest to latest (current listed items aren't in descending order)将提交的列表按从早到晚的降序排列(当前列表项未按降序排列)
  3. From there, determine if a single ID (for an arcade system) has more than 5 scores above 20 (True, 6 for Arcade ID 501 in the example) and if 5 scores exactly are within a 2 minute period (which there are 5) and print the earliest Date-Time and Arcade ID with a score above 20 points ( 20220104 13:12:07.001 ).从那里,确定单个 ID(对于街机系统)是否有 5 个以上的分数高于 20(真实,示例中的 Arcade ID 501 为 6)以及是否有 5 个分数恰好在 2 分钟内(有 5 个)打印得分高于 20 分 ( 20220104 13:12:07.001 ) 的最早日期时间和 Arcade ID。
  4. I also need to reformat the Date-Time, but that's another issue I'd rather tackle later.我还需要重新格式化日期时间,但这是我宁愿稍后再解决的另一个问题。

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数组中,我不确定如何:

  1. Check that at least 5 times in the Array are within a 2 minute period检查阵列中至少有 5 次在 2 分钟内
  2. Print the earliest Date-Time within the 2 minute period打印 2 分钟内最早的日期时间
  3. Printing out the Arcade ID with at least 5 times in the Array within a 2 minute period在 2 分钟内在 Array 中至少打印 5 次 Arcade ID

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方法isEqualisBeforeisAfter进行比较。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM