是否可以仅使用一个for循环而不是2来执行以下操作:
int index = ws.getLastRowNum() + 1;
List<AdditiveList> list=new ArrayList<>();
for(int i=1; i<index; i++){
list.add(new AdditiveList());
}
for(AdditiveList x: list){
Row row=null;
if (rowIterator.hasNext())
row=rowIterator.next();
x.inputAdditiveData(row);
x.outputData();
}
最佳答案
我认为有可能。
尝试这个 -
int index=ws.getLastRowNum()+1;
List<AdditiveList> list=new ArrayList<>();
for(int i=1; i<index; i++){
AdditiveList additiveList = new AdditiveList();
Row row = null;
if(rowIterator.hasNext())
row = rowIterator.next();
additiveList.inputAdditiveData(row);
additiveList.outputData();
list.add(additiveList);
}
如果
rowIterator.hasNext()
返回false,则列表将添加空值。如果那是正确的,而不是按照要求,那么您应该省略如下所示的空值-for(int i=1; i<index; i++){
if(rowIterator.hasNext()){
Row row = rowIterator.next();
AdditiveList additiveList = new AdditiveList();
additiveList.inputAdditiveData(row);
additiveList.outputData();
list.add(additiveList);
}
}