简体   繁体   中英

Iterate through arraylist and assign value for first and last value that fulfills requirement

I have a method that passes in an arraylist of data and a printwriter to print my output into a csv file. The name of the arraylist is records.

for (String record: records){
 String[] recordSplit = record.split(",");
 double value = Double.parseDouble(recordSplit[2]);

 writer.println(record);
 }

As it iterates through the arraylist, the codes are supposed to print onto the csv file line by line. (String record) However, what I want to do is as it loops, I want to check for value that is less than 20 value <= 20 and the first value in the arraylist to fulfill that condition will have another column in the csv file output eg

record += ",Start Value";

and then the iterator reaches the LAST value that fulfills the condition it will have an extra column like so:

record += ",End Value";

在此处输入图片说明

I illustrated the concept in the image above. This is the arraylist, "id, name, value" there are some more attributes that are irrelevant to the question behind value so I'll leave it out. Anyways, value = 100 can be treated like a "forbidden value", so the moment when right before when the loop reaches the end of the arraylist, OR sees the value 100 again, that value will be assigned "End" value. can this actually be done? i'm quite an amateur at java so please be kind, thanks!

Anyways, the output should look like this:

在此处输入图片说明

Here a simplified but working example from which you could start. The snippet uses the example values from Kevins comment.

Here a short explanation how the different states are determined

end value

  • it's the last value to process
  • or we found already the start value and the next value is bigger then the upper bound value

start value

  • we found already the start indicator but not the start value
    and the current value is less or equal to the upper bound value


example snippet

public static void main(String[] args) {
    int[][] samples = {
        {100, 20, 12, 15, 19},
        {45, 5, 100, 4, 12, 100, 6},
        {2, 3, 100, 4, 5, 60}
    };
    outputValues(samples);
}

private static void outputValues(int[][] samples) {
    int startIndicator = 100;
    int upperBound = 20;
    for (int[] sample : samples) {
    int startIndicator = 100;
    int upperBound = 20;
    for (int[] sample : samples) {
        System.out.println("---------------");
        boolean startIndicatorFound = false;
        boolean startValueFound = false;
        boolean endValueFound = false;
        for (int i = 0; i < sample.length; i++) {
            int value = sample[i];
            System.out.printf("%d", value);
            if (endValueFound) {
                // do nothing
            } else if (i == (sample.length - 1)
                    || startValueFound && sample[i + 1] > upperBound) {
                endValueFound = true;
                System.out.print(",End Value");
            } else if (startIndicatorFound && !startValueFound
                    && value <= upperBound) {
                startValueFound = true;
                System.out.print(",Start Value");
            } else if (!startIndicatorFound && value == startIndicator) {
                startIndicatorFound = true;
            }
            System.out.println("");
        }
    }
}

output

---------------
100
20,Start Value
12
15
19,End Value
---------------
45
5
100
4,Start Value
12,End Value
100
6
---------------
2
3
100
4,Start Value
5,End Value
60

There are still edge cases which might not be covered by the current example.

This should get you started:

Boolean started = false; // To keep track if we already wrote "Start Value"
for (int i = 0; i < records.length; i++) {
    String record = records.get(i);

    String[] recordSplit = record.split(",");
    double value = Double.parseDouble(recordSplit[2]);

    if(value < 20 && ! started) { // Starting here
        writer.println(record + ",Start Value");
        started = true;
        continue; // Go to next record
    }

    if(i < records.length - 1) { // There is a next value
        if(Double.parseDouble(records.get(i + 1).split(",")[2]) < 20) {
            // Next value is also smaller than 20, so keep going
            writer.println(record);
        } else {
            writer.println(record + ",End Value");
            started = false; // So we can start again later
    } else {
        writer.println(record);
    }
}

(Not tested)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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