gpt4 book ai didi

java - Java中无法使用Object将字符串添加到List中

转载 作者:行者123 更新时间:2023-11-30 03:24:05 25 4
gpt4 key购买 nike

我正在开发一个基于 JSF 的 Web 应用程序,我从文件(转储文件)中读取内容,然后使用逻辑对其进行解析,并使用对象不断将其添加到列表中,并使用该对象设置字符串。但我不断收到此错误。我很困惑我哪里错了。我是初学者,所以有人可以帮助我吗?

List<DumpController> FinalDumpNotes;
public List<DumpController> initializeDumpNotes()
throws SocketException, IOException {
PostProcessedDump postProcessedDump = (PostProcessedDump) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("postProcessedDump");
List<DumpController> FinalNotes = new ArrayList<>();
if (postProcessedDump.getDumpNotes() == null) {
dumpNotes = new DumpNotes();
}
DumpListController dlcon = (DumpListController) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dumpListController");
DumpInfo dumpinfo = dlcon.getSelectedDumpInfo();
String fileName = dumpinfo.getDate() + dumpinfo.getTime() + dumpinfo.getSeqNo() + dumpinfo.getType() + dumpinfo.getTape() + dumpinfo.getDescription() + ".txt";
if (checkFileExistsInWin(fileName)) {
postProcessedDump.setDumpnotescontent(getFileContentsFromWin(fileName));
String consolidateDumpnotes = getFileContentsFromWin(fileName);
String lines[];
String content = "";
lines = consolidateDumpnotes.split("\\r?\\n");
List<String> finallines = new ArrayList<>();
int k = 0;
for (int i = 0; i < lines.length; i++) {
if (!lines[i].equalsIgnoreCase("")) {
finallines.add(lines[i]);
k++;
}
}
for (int j = 0; j < finallines.size(); j++) {
if (finallines.get(j).startsWith("---------------------SAVED BY")) {
PostProcessedDump dump = new PostProcessedDump();
dump.setDumpMessage(content);
content = "";
FinalDumpNotes.add(dump);
} else {
content = content + finallines.get(j);
}
}
}
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("postProcessedDump", postProcessedDump);
return FinalDumpNotes;
}

我收到以下错误: enter image description here

最佳答案

如果您想将 PostProcessedDump 类型的实例添加到 List 中,您应该更改它的类型。另外,不要忘记初始化它。比如,

List<PostProcessedDump> FinalDumpNotes = new ArrayList<>();

此外,Java 命名约定是以小写字母开头的变量名称。 FinalDumpNotes 看起来像一个类,我建议类似的东西

 List<PostProcessedDump> processedList = new ArrayList<>();

关于java - Java中无法使用Object将字符串添加到List中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30695094/

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