Java从数组列表生成子列表并存储到对象列表的数组列表中
我有一张物品清单。我想从arraylist生成子列表。。根据文件大小 例如:Java从数组列表生成子列表并存储到对象列表的数组列表中,java,arrays,list,Java,Arrays,List,我有一张物品清单。我想从arraylist生成子列表。。根据文件大小 例如: [{ "name": "food_image1.jpeg", "fileSize": 10 }, { "name": "food_image2.jpeg", "fileSize": 20 }, { "name": "food_image3.jpeg", "fileSize": 30 }
[{
"name": "food_image1.jpeg",
"fileSize": 10
},
{
"name": "food_image2.jpeg",
"fileSize": 20
},
{
"name": "food_image3.jpeg",
"fileSize": 30
},
{
"name": "food_image4.jpeg",
"fileSize": 10
}
]
我的固定尺寸是:40
因此,我的子列表将是:
[
[{
"name": "food_image1.jpeg",
"fileSize": 10
},
{
"name": "food_image2.jpeg",
"fileSize": 20
},
{
"name": "food_image4.jpeg",
"fileSize": 10
}
],
[{
"name": "food_image3.jpeg",
"fileSize": 30
}]
]
因此,我有一个mojo对象FileInfo.java,它有两个属性
public class FileInfo{
private String name;
private Long fileSize;
//setter and getters
}
我的逻辑是:
public List<List<FileInfo>> getAllSublistFromList(){
//fixed size data
long fixedPartSize =40;
long tempSize = 0;
long finalSize = 0;
//here I am fetching all the list
List<FileInfo> fileInfoList = jsondataList();
List<FileInfo> makeSublist = new ArrayList<>();
//Initializing list of DriveFileInfo object within a list.
List<List<FileInfo>> allDataAspart = new ArrayList<>();
//iterating arraylist of DriveFileInfo object
for (int i = fileInfoList.size() - 1; i >= 0; i--) {
//first fetching filesize of every DriveFileInfo object
tempSize = fileInfoList.get(i).getFileSize();
//checking if the file exist or not
if (tempSize >= 1) {
//if the file exist keep it as finalSize.
finalSize = finalSize + tempSize;
//now checking new size is greater than fixed size or not.
if (finalSize <= fixedPartSize) {
//if the finalSize is lessar than fixedSize then add to ArrayList,named makeSublist
makeSublist.add(fileInfoList.get(i));
//after adding to the list remove index from makeSublist Arraylist
fileInfoList.remove(i);
} else {
finalSize = 0;
//if the finalSize is greater than fixedSize then add to ArrayList of Arraylist,named allDataAspart
allDataAspart.add(makeSublist);
//and clean the arraylist means initialize makeSublist arraylist data
makeSublist = new ArrayList<>();
}
}
//checking if the file not exist
else {
//remove the index from fileInfoList arraylist
fileInfoList.remove(i);
}
}
allDataAspart.add(makeSublist);
//returning the arraylist of List object data.
return allDataAspart;
}
公共列表getAllSublistFromList(){
//固定大小数据
长固定零件尺寸=40;
长tempSize=0;
长期最终化=0;
//我在这里取所有的清单
List fileInfoList=jsondataList();
List makeSublist=new ArrayList();
//正在初始化列表中DriveFileInfo对象的列表。
List alldataaspat=new ArrayList();
//正在迭代DriveFileInfo对象的arraylist
对于(int i=fileInfoList.size()-1;i>=0;i--){
//首先获取每个DriveFileInfo对象的文件大小
tempSize=fileInfoList.get(i).getFileSize();
//检查文件是否存在
如果(tempSize>=1){
//如果文件存在,则将其保留为最终版本。
最终规模=最终规模+临时规模;
//现在检查新大小是否大于固定大小。
如果(最终化这个怎么样:
public List<List<FileInfo>> partition(List<FileInfo> list, int maxSize) {
List<List<FileInfo>> result = new ArrayList<>();
List<FileInfo> current = new ArrayList<>();
int currentSize = 0;
for (FileInfo fi: list) {
if (fi.getFileSize() > 0) {
if (!current.isEmpty()
&& currentSize + fi.getFileSize() > maxSize) {
result.add(current);
current = new ArrayList<>();
currentSize = 0;
}
current.add(fi);
currentSize += fi.getFileSize();
}
}
if (!current.isEmpty()) {
result.add(current);
}
return result;
}
公共列表分区(列表列表,int-maxSize){
列表结果=新建ArrayList();
当前列表=新的ArrayList();
int currentSize=0;
用于(文件信息fi:列表){
如果(fi.getFileSize()>0){
如果(!current.isEmpty()
&¤tSize+fi.getFileSize()>maxSize){
结果。添加(当前);
当前=新的ArrayList();
currentSize=0;
}
当前。添加(fi);
currentSize+=fi.getFileSize();
}
}
如果(!current.isEmpty()){
结果。添加(当前);
}
返回结果;
}
代码中的fileInfoList
是什么?代码中似乎有一些奇怪的情况。我不确定我是否理解finalSize+tempSize
部分。这可能是设计问题,也可能需要解释。另一种代码味道是变量makeSubList
。因为变量是动词,所以它表示您需要uld对此有一个方法。尝试将此大方法拆分为至少3个较小的方法(由于if的嵌套结构,有3个方法),很好的名字。我确信问题会自行解决。另外,在使用的范围的开头定义变量,而不是在方法的开头!为什么要从输入列表中删除文件?@dey fileInfoList是数组列表,它调用“jsondataList()”方法来获取所有arraylist