gpt4 book ai didi

java - 列表中始终显示最后一个元素的信息(Java)

转载 作者:行者123 更新时间:2023-12-02 04:35:13 25 4
gpt4 key购买 nike

我有一些列表,其中包含一些 Apk 文件的信息:

static ContentAndDAO contentAndDao = new ContentAndDAO();

public static void main(String[] args)
{
int manifestNum;
long contentId = 111111;
long devFileId = 222222;
List<DevFile> fileList;
List<DevSupport> supports = null;
List<ContentDev> contentList = new ArrayList<ContentDev>();
ContentDevDAO contentDevDao = new ContentDevDAO();
DevFileDAO devFileDao = new DevFileDAO();
ManifestMethods manifestMethods = new ManifestMethods();
DevFile apkFile = null;

try
{
manifestNum = 1;

File dir = new File("C:\\Users\\lenovo 01\\Desktop\\basari\\buulkcontent\\klasorlenen");
String[] extensions = new String[] {"apk"};
List<File> files = (List<File>) FileUtils.listFiles(dir, extensions, true);
Collections.sort(files);
for(File file : files)
{
apkFile = new DevFile();
fileList = new ArrayList<DevFile>();

if(file.getName().contains(".apk"))
{
FileInputStream fis = new FileInputStream(new File(file.getAbsolutePath()));
String apkMd5 = DigestUtils.md5Hex(fis);
fis.close();

System.out.println(file);
System.out.println(file.length());
System.out.println(apkMd5.toUpperCase());
System.out.println(contentId);

apkFile.setByteSize(file.length());
apkFile.setUrl("/file/getContent/" + contentDevDao.createId(contentId) + "/" + apkMd5.toUpperCase() + "/apk");
apkFile.setThumbnailUrl("/file/getContent/" + contentDevDao.createId(contentId) + "/" + apkMd5.toUpperCase() + "/apk");
apkFile.setDeleteUrl("/file/deleteContent/" + contentDevDao.createId(contentId) + "/" + apkMd5.toUpperCase() + "/apk");
apkFile.setFileHash(apkMd5.toUpperCase());
apkFile.setFilePath("content/" + contentDevDao.createId(contentId) + "/" + apkMd5.toUpperCase() + ".apk");
apkFile.setFileName(manifestMethods.getApplicationName(manifestNum).replaceAll(" ", "-") + ".apk");
apkFile.setName(manifestMethods.getApplicationName(manifestNum).replaceAll(" ", "-"));
apkFile.setPackageVersion(manifestMethods.getVersionName(manifestNum));
apkFile.setPackageName(manifestMethods.getPackageName(manifestNum));
apkFile.setPackageVersionCode(manifestMethods.getVersionCode(manifestNum));
apkFile.setSdkVersion(manifestMethods.getSdkVersion(manifestNum));

contentId++;
devFileId++;
manifestNum++;
}
}

for(int y = 1; y <= 53; y++)
{
manifestNum = 1;
fileList = new ArrayList<DevFile>();

DevFile file = new DevFile();
ContentDev content = new ContentDev();

/* some DevFile file addings */

fileList.add(file);
content.setDevFiles(fileList);
contentList.add(content);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

当我将信息一一打印到控制台时,它按照我想要的方式显示。但在列表中,它总是只显示最后添加的 apk 文件的包名、字节大小、哈希值等。当然我不希望这样。怎么了?

注意:请不要介意代码缺乏可读性和模块化。我是面向对象结构的新手。

最佳答案

您在循环的每次迭代中创建一个新列表:

    for(int y = 1; y <= 53; y++)
{
fileList = new ArrayList<DevFile>();
DevFile file = new DevFile();
ContentDev content = new ContentDev();

/* some DevFile file addings */

fileList.add(file);
...

这意味着只有最后一个文件才会出现在该列表的最后。

将其更改为:

    fileList = new ArrayList<DevFile>();
for(int y = 1; y <= 53; y++)
{
DevFile file = new DevFile();
ContentDev content = new ContentDev();

/* some DevFile file addings */

fileList.add(file);
...

此外,我发现您在另一个循环中创建了 DevFile 实例,但从未对它们执行任何操作。难道不应该将它们添加到列表中吗?

关于java - 列表中始终显示最后一个元素的信息(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30915772/

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