我有一排像这样的arraylist数据
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 | - |
或以arraylist的形式会是这样,
[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);
}
,但这只会导致第一个元素缺失。它仍然缺少它的下一个元素。请帮助我。
答案 0 :(得分:1)
您可以在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);
}
答案 1 :(得分:1)
如果要忽略第一列和天列
for (int i = 1; i < r.getLastCellNum(); i++) {
Cell cell = r.getCell(i);
String val = formatter.formatCellValue(cell);
if (!val.startsWith("DAY")) {
al.add(val);
}
}