gpt4 book ai didi

java - 仅使用 1 个 for 循环

转载 作者:行者123 更新时间:2023-12-01 18:36:20 26 4
gpt4 key购买 nike

是否可以仅使用一个 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,则列表将添加 null 值。如果这是正确的,不符合要求,那么您应该省略 null,如下所示 -

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);
}
}

关于java - 仅使用 1 个 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21845020/

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