我构建了一个非常慢的对象列表,我需要提高构建列表的速度。添加到列表中的对象本身具有多个子对象,通常为 3 个对象,根据剩余要添加的数据量更改为 2 或 1。
我打算构建一个子对象数据 bean,因为传递的参数大部分相同,只有子对象发生变化。我相信有一种比我列出清单的方式更有效的方法。
这是我现在的代码:
List<MainObject> list = new ArrayList();
List<Data> dataList = getDataList();
MainObject mainObject;
int limit = 100;
for(int i = 0; i < limit; i += 3) {
if(i == limit - 1) {
list.add(mainObject = new MainObject(new SubObject(dataList.get(i), this, bBool, tabs)));
}else if(i == limit - 2) {
list.add(mainObject = new MainObject(new SubObject(dataList.get(i), this, bBool, tabs),
new SubObject(dataList.get(i +1), this, bBool, tabs)));
}else {
list.add(mainObject = new MainObject(new SubObject(dataList.get(i), this, bBool, tabs),
new SubObject(dataList.get(i +1), this, bBool, tabs),
new SubObject(dataList.get(i +2), this, bBool, tabs)));
}
}
return list;
参数“this、bBool、tabs”都是类变量并且始终相同。我确实知道最后一个 else 条件在大多数情况下都会运行,因此这可能应该是 if 语句中的第一个条件,但我宁愿找到一种更好的方法来一起构建列表。
德玛西亚99
哆啦的时光机
慕尼黑8549860
相关分类