简体   繁体   English

数组列表<string>检查满足条件的第一个和最后一个值

[英]java arraylist<string> check for the first and last value that fulfills a condition

I have an arraylist of strings, called ArrayList<string> records that stores strings of data that will be delimited by commas , .我有一个字符串ArrayList<string> records ,称为ArrayList<string> records ,用于存储将由逗号,分隔的数据字符串。 At the [5] position of the string (after being delimited), will be a value between 0 to 500. An example of a string of that data is as follows:在字符串的 [5] 位置(分隔后),将是 0 到 500 之间的值。该数据的字符串示例如下:

userA, 1418600437, 38.9047, 177.0164, washington, 180

so anyway, the arraylist will have many strings of similar data.所以无论如何,arraylist 将有许多类似数据的字符串。 What I need is to iterate through the arraylist (which is already sorted in the sequence I want), then find the FIRST value in the arraylist that fulfills the criteria that the number at position[5] is < 200 and then the LAST value of the arraylist that fulfills the same criteria.我需要的是遍历数组列表(已经按我想要的顺序排序),然后在数组列表中找到满足位置 [5] 处的数字< 200第一个值,然后找到最后一个值满足相同条件的数组列表。 When the first value is detected, a String will be assigned to that one record to call it the "start" value and the same for the last value.当检测到第一个值时,将向该记录分配一个字符串以将其称为“开始”值,最后一个值也是如此。 Can anyone help me with the logic, or just a pseudocode of how that might work?任何人都可以帮助我解决逻辑问题,或者只是一个可能如何工作的伪代码?

Try the following code.试试下面的代码。 The first value to match the criteria is recorded using a boolean flag.使用boolean标志记录与条件匹配的第一个boolean The last value is updated each time the criteria matches, with the result that after the for loop finishes, the variable lastValue will store the last matching value.每次条件匹配时都会更新最后一个值,结果是在for循环完成后,变量lastValue将存储最后一个匹配值。

boolean seenFirst = false;
int firstValue, lastValue;

for (String record : records) {
    String[] parts = record.split(", ");
    int value = Integer.parseInt(parts[5]);

    if (value < 200)) {
        lastValue = value;
        if (!seenFirst) {
            seenFirst = true;
            firstValue = value;
        }
    }
}

If you are using Java 8, this is very simple:如果您使用的是 Java 8,这很简单:

List<string> records = new ArrayList<>();
String firstValue = records.stream().filter(s -> Integer.parseInt(s.split(",")[5].trim()) < 200)
                                    .findFirst().get();
String lastValue = IntStream.range(0, records.size())
                            .mapToObj(i -> records.get(records.size() - i - 1))
                            .filter(i -> Integer.parseInt(s.split(",")[5].trim()) < 200)
                            .findFirst().get();

To get the first value, we get the integer, filter those that are less than 200 and retrieve the first value.要获取第一个值,我们获取整数,过滤小于 200 的那些并检索第一个值。

To get the last value, it is the same but the iteration is done in reverse order.要获得最后一个值,它是相同的,但以相反的顺序进行迭代。

As I stated in the comments: first do an iteration to get the first matching record, then do a reverse iteration to get the last matching record.正如我在评论中所述:首先进行迭代以获取第一条匹配记录,然后进行反向迭代以获取最后一条匹配记录。 In both cases, break out of your iterations after you found a match.在这两种情况下,在找到匹配项后中断迭代。

Could look something like this:可能看起来像这样:

String first, last;

for (int i = 0; i < records.size(); i++) {
    if (Integer.parseInt(records[i].split(", ")[5]) < 200) {
        first = records[i];
        break;
    }
}

for (int i = records.size() - 1; i >=0; i--) {
    if (Integer.parseInt(records[i].split(", ")[5]) < 200) {
        last = records[i];
        break;
    }
}

Obviously should be refactored to get rid of the duplicated matching logic.显然应该重构以摆脱重复的匹配逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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