gpt4 book ai didi

java - 如何在for循环中获取前2个元素然后跳过第三个元素java

转载 作者:行者123 更新时间:2023-12-02 01:50:37 26 4
gpt4 key购买 nike

我有一行数组列表数据,如下所示

JANUARY | DAY1 | ABSENT | MC | DAY2 | ABSENT| MC | DAY3 |ACTIVE| - | DAY4 |ACTIVE | - |
FEBRUARY| DAY1 | ACTIVE | | DAY2 | ABSENT| MC | DAY3 |ACTIVE| - | DAY4 |ACTIVE | - |
MARCH | DAY1 | ABSENT | MC | DAY2 | ACTIVE| - | DAY3 |ACTIVE| - | DAY4 |ACTIVE | - |

或者以数组列表的形式是这样的,

[JANUARY,DAY1,ABSENT,MC,DAY2,ABSENT,MC,DAY3,ACTIVE,-,DAY4,ACTIVE,]
[FEBRUARY,DAY1,ACTIVE ,-,DAY2,ABSENT,MC,DAY3,ACTIVE,-,DAY4,ACTIVE,]
[MARCH,DAY1,ABSENT,MC,DAY2,ACTIVE,-,DAY3,ACTIVE,-,DAY4,ACTIVE,-]

因此,我不想存储所有数据,而是想仅存储列 DAY1、DAY2、DAY3、DAY4 之后的数据,如下所示:

[ABSENT,MC,ABSENT,MC,ACTIVE,-,ACTIVE,-]
[ACTIVE,-,ABSENT,MC,ACTIVE,-,ACTIVE,-]
[ABSENT,MC,ABSENT,MC,ACTIVE,-,ACTIVE,]

我当前的循环代码如下所示:

    DataFormatter formatter = new DataFormatter();

String val = null;

for (int rowNum = 1; rowNum < sheet.getLastRowNum(); rowNum++) {
Row r = sheet.getRow(rowNum);

ArrayList<String> al = new ArrayList<String>();


for (int i = 2; i+2 < r.getLastCellNum() + 1; i = i + 3) {
Cell cell = r.getCell(i);
val = formatter.formatCellValue(cell);
al.add(val);
}

System.out.println(al);
}

但这只会导致第一个元素不存在。它仍然缺少下一个元素。请帮助我。

最佳答案

您可以在 for 循环内添加第一个元素和第二个元素。

DataFormatter formatter = new DataFormatter();

String val = null;

for (int rowNum = 1; rowNum < sheet.getLastRowNum(); rowNum++) {
Row r = sheet.getRow(rowNum);

ArrayList<String> al = new ArrayList<String>();


for (int i = 2; i+2 < r.getLastCellNum() + 1; i = i + 3) {
Cell cell = r.getCell(i);
val = formatter.formatCellValue(cell);
al.add(val);
cell = r.getCell(i+1);
val = formatter.formatCellValue(cell);
al.add(val);
}

System.out.println(al);
}

关于java - 如何在for循环中获取前2个元素然后跳过第三个元素java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53029662/

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